There are situations where you want to display Text or Links or any content in a blog post specific to a category. If you are struggling to find a solution then you are in the right page. You don’t need any wordpress plugins, instead by just adding a piece of PHP code may help you to achieve this.

Lets see how to add such category specific texts ? You could add this at any place you want, below the blog post, above the blog post or anywhere else.

Here is the code…

<?php
if ( in_category( ‘affiliate’) )
{
echo ‘<p><strong>Recommended Affiliate Products :</strong>
<ul>
<li style=”font-size=12″>
<a href=”https://www.blasho.com/go/superaffiliate/” rel=”nofollow”>Super Affiliate: How I Made $500,000+ In One Year.</a>
</li>
<li style=”font-size=12″>
<a href=”https://www.blasho.com/go/affilroma/” rel=”nofollow”>Affilorama :: The #1 Affiliate Marketing Training Portal.</a>
</li>
<li style=”font-size=12″>
<a href=”http://https://www.blasho.com/go/infoproduct/” rel=”nofollow”>Info Product Killer : Your Adventure In Success Starts Here</a>
</li>
</ul></p><br/>’;
}
?>

The above code has an “if” condition checking for the specific category using the API in_cateogry(). This API returns true if the passed category matches the current category otherwise, it returns false. You could add any html code using the “echo” call. I used this kind of code to display affiliate products specific to each category. In this example, if the visitor reads an article from category ‘affiliate’, he will see the products related to that category.

The general code snippet looks like:

<?php
if ( in_category(‘<CategoryName>’) )
{
echo ‘<AddYourHTMLCodeHere>’;
}
?>

Just replace the with your own category and with your own HTML code that you want to display specific to this category. If you want to add specific text for each and every category then copy paste this code again and again, change the and accordingly for each category.

Advantages of Category Specific Texts:

  • It helps you to show texts or any links specific to the current category and hence improves contextual readability of your blog posts.
  • It helps in terms of SEO too because the current category, blog posts and the specific text speaks about the same topic. Basically, you could add value to the existing content based on categories.

Single Post (single.php) is the right place to display specific content above or below the blog posts. You could add this code anywhere based on your requirement.

Let me know if this code has any issues !

2017-10-09T20:56:53+00:00

About the Author:

I love-to-code, passionate-to-write, an upcoming author, and WordPress enthusiast. Here at BlashO, I mostly share WordPress tips. Connect with me @Twitter.

6 Comments

  1. Thanks for the code! You mention that you could add the text before or after the content. Could you please explain how you determine WHERE the text will appear.

    • Ven Tesh - Reply

      Sam, it depends on where you put this code. Check for the function the_content() in your single.php file, you can add before or after to it.

  2. Sorry.. another question – if I repeat this code about 10 times (for different categories) will it slow down my site?

    • Mr. Ven - Reply

      i believe it may not increase the load time much because there is only a if() condition in this code…if you feel an increase in load time simply use a cache plugin (say hyper cache or wp super cache)…done ๐Ÿ™‚

  3. You would think that someone would write a plugin for this. But I haven’t found one anywhere.

    • Ven Tesh - Reply

      That’s right, Tony. I don’t find a plugin and hence I put this blog post.

Leave A Comment