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…
if ( in_category( ‘affiliate’) )
echo ‘<p><strong>Recommended Affiliate Products :</strong>
<a href=”https://www.blasho.com/go/superaffiliate/” rel=”nofollow”>Super Affiliate: How I Made $500,000+ In One Year.</a>
<a href=”https://www.blasho.com/go/affilroma/” rel=”nofollow”>Affilorama :: The #1 Affiliate Marketing Training Portal.</a>
<a href=”http://https://www.blasho.com/go/infoproduct/” rel=”nofollow”>Info Product Killer : Your Adventure In Success Starts Here</a>
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:
if ( in_category(‘<CategoryName>’) )
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 !