• Resolved bowend3c

    (@bowend3c)


    Hello!

    I am trying to output a shortcode if a post belongs to a certain category.

    Would this work? If so, where should I put it so that it will work?

    
    <?php
    	if (has_category('oldsite')) {
    	    echo do_shortcode( '[types field='body'][/types]' );
    	else {
    		// do nothing
    	}
    ?>
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    That would work. Put it in single.php (if your theme has one).

    Thread Starter bowend3c

    (@bowend3c)

    Update: For anyone who reads this thread in the future. I used the wrong quotes. I should have done double quotes, not single quotes. Likewise the “else” part is completely unnecessary. This is the revised code:

    
    <?php
    		if (has_category('oldsite')) {
    			echo do_shortcode( "[types field='body'][/types]" );
    		}
    		?>
    

    Now it works!

    • This reply was modified 8 years ago by bowend3c.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Output shortcode if post is from a certain category.’ is closed to new replies.