Add text below every post

Hi there.

I’m trying to add a short line of text to appear at the end of every blog post. I tried adding the following to functions.php, but it broke the site:

add_filter('the_content','add_my_content');
function add_my_content($content) {
$my_custom_text = '<h5 style='font-weight:400; color:#dd5500; text-align:center;' class='mtm mbm'>CUSTOM TEXT HERE <a href="https://custom-url-here" title="link-title-here" target="_blank">FIND OUT WHY</a></h5>'; //
if(is_single() && !is_home()) {
$content .= $my_custom_text;
}
return $content;
}

Was I at least close? :slight_smile:

Hi There,

Please update your code to this:

add_action( "x_after_the_content_end", "add_post_shortcode" );
function add_post_shortcode() {
	if(is_singular( 'post' )){
		echo '<h5 style=\'font-weight:400; color:#dd5500; text-align:center;\' class=\'mtm mbm\'>CUSTOM TEXT HERE <a href="https://custom-url-here" title="link-title-here" target="_blank">FIND OUT WHY</a></h5>';
	}
}

Aha! Seems like I wasn’t even close! That’ll teach me to try to be clever ( :grinning: )

Position is nearly how I want it: What I forgot to mention is that I already have a function that displays the Author field on posts.

add_filter( 'the_content', 'custom_author_code');

function custom_author_code($content) {
   if ( is_single() ) {
      return $content . do_shortcode('[author title="About the Author"]');
   } else {
      return $content;
   }
}

I need the above to render below this new one I’m adding. How do I do that?

Thanks in advance

Hi there,

Please try this:

add_action( "x_after_the_content_end", "add_post_shortcode" );

function add_post_shortcode() {
	if(is_singular( 'post' )){
        echo do_shortcode('[author title="About the Author"]');
		echo '<h5 style=\'font-weight:400; color:#dd5500; text-align:center;\' class=\'mtm mbm\'>CUSTOM TEXT HERE <a href="https://custom-url-here" title="link-title-here" target="_blank">FIND OUT WHY</a></h5>';
	}
}

Hope this helps.

Hmm, that gave me two instances of the “About the Author”.

Let me backtrack: At the moment at the end of every post I have some social sharing buttons (called from wp-single.php), then I have the “About the Author” section, called from functions.php. What I want is the new text ‘add_post_shortcode’ to appear below the social sharing buttons, but above the About the Author.

Hi There,

To avoid confusion and duplicate, let’s add everything using functions.php like below:

add_action( "x_after_the_content_end", "add_post_shortcode" );

function add_post_shortcode() {
	if(is_singular( 'post' )){
        echo do_shortcode('[share title="Share this Post" facebook="true" twitter="true" google_plus="true" linkedin="true" pinterest="true" reddit="true" email="true"]');
		echo '<h5 style=\'font-weight:400; color:#dd5500; text-align:center;\' class=\'mtm mbm\'>CUSTOM TEXT HERE <a href="https://custom-url-here" title="link-title-here" target="_blank">FIND OUT WHY</a></h5>';
        echo do_shortcode('[author title="About the Author"]');

	}
}

No need to add code on wp-single.php just that code. See how we called sharing shortcode first, then custom code and then last the author shortcode.

Hope this helps.