2

I would like to include some text at the end of every post. Like how some people write "Like my posts? Follow me on twitter!" or something like that. There are some buttons for users to tweet or share the posts, but I want to see how people would respond to explicit suggestions.

I thought maybe I can just copy-paste it over and over again, but if I ever decide to change it, I'd have to update every one of them, so that isn't a very scalable solution.

I also have existing posts that I wish to have this text appear.

I'm thinking of changing the theme so that each post has the text, but that doesn't seem like a very "wordpress" way to do it.

What is a good way to accomplish this? Being able to choose which posts have the text or not (eg: based on category, tags, etc.) would be nice.

2 Answers 2

3

You can try the the_content filter, to append/prepend text to the post content.

Here's a simple example:

add_filter( 'the_content', function( $content ) {

    if( is_single() && has_tag( 'follow-me-on-twitter' ) )
    {
        $content .= '<div>Like my posts? Follow me on twitter!</div>';
    }
    return $content;

}, 99 );

where we append the extra text if the current single post has the follow-me-on-twitter tag.

Other ideas:

  • You can do something similar for categories with the has_category() function.

  • You might consider adding an extra taxonomy to your posts, that's not visible on your site, to handle the markings. Then you need has_term() instead.

  • You can use the post meta (custom fields) to mark each post. Maybe add a meta box with a select box.

  • ...

1

What is referred to as a "template part" seems to fit exactly your need here.

WP Template Part ref

Simply write up the snippet that produces the message you would like to repeat in a php file and call it with <?php get_template_part( $slug, $name ); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.