Support » Fixing WordPress » Need help with conditional statement to add content to certain category posts

  • Resolved bigcityjohn

    (@bigcityjohn)


    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');
Viewing 2 replies - 1 through 2 (of 2 total)
  • Michael

    (@alchymyth)

    Forum Moderator

    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:
    http://codex.wordpress.org/Conditional_Tags

    bigcityjohn

    (@bigcityjohn)

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Need help with conditional statement to add content to certain category posts’ is closed to new replies.