WebCraft Tools

  • News
  • WordPress
    • WordPress Basics
    • WordPress Advanced
    • WordPress Performance
    • WordPress Security
    • WordPress Commerce
    • WordPress Troubleshooting
    • WordPress Themes
  • SEO and Analytics
WebCraft Tools › WordPress Advanced › Easily add text to post title based on category

Easily add text to post title based on category

Last updated on April 19, 2017 by Editorial Staff

Have you ever wanted to add a little specification to your post titles if they were in a certain category. For instance this post. I have it filed under “Quick Tip”. What if I wanted to add the text “Quick Tip:” in front of every post in that category. It’s pretty simple.

[php]

add_filter( ‘the_title’, ‘my_modify_title’, 10, 2 );
function my_modify_title( $title, $id ) {
if( in_category( ‘Quick Tip’, $id ) ) {
$title = ‘Quick Tip: ‘ . $title;
}
return $title;
}

[/php]

Add this code to your site functionality plugin and change it for your particular category and you are all set. I even wrapped the added text in a span in case you wanted to style it to make it stand out.

One thing to keep in mind. You can do an if/else in here to do this for several categories if you like but it certainly works best if you are assigning one category per post. Otherwise it will simply use the first match.

Tweet
Pin
Share
0 Shares

Filed Under: WordPress Advanced

Comments

  1. Jo says

    January 18, 2013 at 3:20 pm

    Simple and very useful. Thanks!

    Reply
    • James Laws says

      January 18, 2013 at 3:22 pm

      Thanks for stopping by and commenting. Glad you found it useful.

      Reply
  2. Excess says

    April 24, 2013 at 2:31 pm

    what do you mean by “functionality plugin” tried to insert it into my function.php file but to no avail. Thanks

    Reply
    • James Laws says

      April 25, 2013 at 6:49 am

      A functionality is a custom plugin you create to place all your custom code into. It’s much better than putting it in your themes functions.php file because if you ever change themes the plugin is still intact. Here is a quick tutorial on how to go about that.

      Reply

Leave a Reply Cancel reply

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

Site Links

  • Contact Us
  • Disclosure Statement
  • Privacy Policy

About WebCraft.tools

WebCraft.tools is your free resource for building, maintaining, and improving your WordPress website. Whether you are building your first site or have been building them for years, WebCraft.tools aims to provide you with tips, tricks, tutorials, and reviews to take your WordPress site to the next level.

© 2017 - 2021 · WebCraft Tools · Built by the WP Ninjas, LLC