I would like to show specific content within a post if the post is listed under a specific category.

For example, if we're on the Dallas post and it is under the 'Dallas' category, then I want specific text to show on the Dallas page via a single.php code insert.


echo <<<insert
<?php if ( in_category( 'dallas-slug') ) : ?>
custom text to insert
<?php endif; ?>

Whenever I do this, it shows the text across all my post, ignoring the actual category slug. I'd like for the text to only show on posts where the 'dallas-slug' category is used.

  • Not sure if this has an impact, but this is within other code on the single.php page which generates listings for a directory based website. I'm trying to include a 'premium' listing at the top before other normal listings are shown. – user1858780 Jun 12 '18 at 17:34
  • Additionally, the code pipes in listings per city/state using the same single.php file so I'm thinking it's ignoring my category call/check in this case since it's within other code that already checked it. Is that a possibility, and if so, is there a way to recheck category for the code I'm trying to insert? – user1858780 Jun 12 '18 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.