[resolved] Need help with conditional statement to add content to certain category posts (3 posts)

  1. bigcityjohn
    Posted 5 years ago #

    I'm trying to add some content to the end of each post's content (and before comments) but only if that post is in a particular category (category "XYZ").

    Here's the conditional code that I've come up with (in my very limited capabilities) in my functions.php file, but it's not outputting anything after the content in category XYZ posts.

    Any ideas where I've gone wrong? Or, any better way of writing the code?

    function add_post_content($content) {
    	if(is_single() && is_category('XYZ')) {
    		$content .= 'This is the text that will automatically get inserted after the post if it's in category XYZ.';
    	return $content;
    add_filter('the_content', 'add_post_content');
  2. Michael
    Forum Moderator
    Posted 5 years ago #

    this line if(is_single() && is_category('XYZ')) will never get true, because is_category() checks for a category archive.

    use in_category('XYZ') if you are checking in single posts.

    if you are working with wordpress template tags, consult the codex:

  3. bigcityjohn
    Posted 5 years ago #

    Changing "is_category" to "in_category" seems to have done the trick! Thanks for the help.

Topic Closed

This topic has been closed to new replies.

About this Topic