0
votes

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.

single.php

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

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.

Note: I'm 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 that are based on State categories. The code in the single.php file pipes in listings per city/state using the same single.php file I'm using for this extra code, 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?

2
Remove the echo part i.e. the first and last line and it should work. - Alec Smart
You should paste the entire single.php if you have another code that may be in conflict with this. - Klian
It throws up a Parse error: syntax error, unexpected '<' error when I remove the echo. - user1858780
@Klian - I've added the full code to the initial post. Thanks! - user1858780
I figured it out - had to start with if and not <?php since that was already part of the code earlier. - user1858780

2 Answers

0
votes

Your code should just be:

<?php if ( in_category( 'dallas-slug') ) : ?>
<p>
custom text to insert
</p>
<?php endif; ?>
0
votes

There was already code with <?php in the file so I didn't need it again. I ended up doing this:

if ( in_category('dallas-slug') ) { 
 echo '<p>
custom text to insert
</p>';
 }