WordPress Snippet: display custom text after the title of an article in a specific category

Reminder: here is a small snippet of code to add to your functions.php file in order to display a small text tag just after the title of an article, if it belongs to a particular category.

This is useful when, for example, you want to emphasize the fact that this article belongs to a particular category. I use it in particular when I want to make micro-articles of a few lines, in order to make the reader understand that it is a “short” of two or three lines, and not a real developed article, or when I want to display the label “Members Zone” for private articles reserved for members, as in the example below.

In the example below, you just need to replace the words “shorts” and “Members area” with the categories of your choice.

function label_after_post_title($title, $post_id) {
if (is_admin()) {
return $title;
}

if ( in_the_loop() && has_category( 'shorts', $post_id ) ) {
return $title . &#039; • <span class=" »shorts-category »">[SHORTS]</span>&#039;;
}

if ( in_the_loop() && has_category( 'members', $post_id ) ) {
return $title . &#039; • <span class=" »shorts-category »">[MEMBERS AREA]</span>&#039;;
}

return $title;
}
add_filter('the_title', 'label_after_post_title', 10, 2);

Don't miss business advice by email

No spam, just an email when new content is published.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

WordPress is the content management system of choice for online website builders. About 30% of the internet platforms currently available online are built around this system which has the share https://buff.ly/4gTcUuk

🫘 Flageolet beans, white beans, lentils, broad beans, chickpeas and other legumes are the best “slow carbohydrates” for health.
👉 Discover the superpowers of legumes in their dedicated content on Blooness: https://buff.ly/4b4nnlc

🍞 Sourdough bread with ancient flours is a more digestible alternative to traditional bread.
Natural fermentation can improve digestion and nutrient bioavailability.
👉 Learn more about the best breads at http://blooness.com

Load more

Don't miss business advice by email

No spam, just an email when new content is published.

en_USEN