0

I wish to add a small icon/badge left to my blog post titles those are new (maybe 5-7 days ago). I may preferably have a blinking gif image for this. Now, how to execute this?

First I tried with this which I got from a WP forum post.

add_filter( 'post_content', 'addBadge2Title' );
function addBadge2Title(){
    $seconds = strtotime( "now" ) - strtotime( get_the_date( "Y/m/d" ) );
    $badge= get_stylesheet_directory_uri().'/library/images/new_ribbon.gif';
    if ( $seconds < 10950400 ) {
        echo '<img class="new_ribbon" width="75"  height="75" src="'.$badge.'" >';
    }
}

But nothing shows up.

Code resource : http://wordpress.org/support/topic/plugin-to-add-a-new-badge-to-title-1?replies=3#post-2499846

5
  • 1
    When you take something from somewhere, then it's under some license. Please link to the original source. Thanks.
    – kaiser
    Sep 9 '14 at 10:50
  • You can add a icon or image before entry titles using CSS. .single .entry-title:before Please ask on Stackoverflow. Otherwise, you need to filter the title however you will still need CSS. Sep 9 '14 at 10:59
  • to be fair it is probably from the wp.org forums wordpress.org/support/topic/… but it looks like there was some C&P bug in the way. Sep 9 '14 at 12:14
  • 1
    I am sorry for this. I had no intention to demean the original author. Maybe I was in a hurry and did not quote the source. I have updated the thread by the way. Thanks for the heads up.
    – kalyan
    Sep 9 '14 at 13:55
  • Your code example demonstrates that you do not understand how filters work, you will need to understand the difference between filters and hooks to fully understand any answer given
    – Tom J Nowell
    Sep 22 '14 at 21:48
1

You want to edit the title, and not the content.

Check this for filters you want to use.

If you want to edit your title you should add_filter to it and modify it(the_title). Like so:

add_filter('the_title', 'addBadge2Title');
function addBadge2Title($title)
{
    $seconds = strtotime("now") - strtotime(get_the_date("Y/m/d"));
    $badge = get_stylesheet_directory_uri() . '/library/images/new_ribbon.gif';
    if ($seconds < 10950400) {
        $title = '<img class="new_ribbon" width="75"  height="75" src="' . $badge . '" >' . $title;
    }
    return $title;
}

Edit: use badges only for specific categories

If you want to use this code for specific categories you have a few options

  1. First remove add_filter('the_title', 'addBadge2Title'); and keep only the function declaration in functiosn.php and in the archive-{$category-slug}.php file add and remove the filter like so

    if(have_posts()) : while(have_posts()) : the_post()
    
        add_filter('the_title', 'addBadge2Title');
        the_title();
        remove_filter('the_title', 'addBadge2Title');
    
    endwhile; endif;
    
  2. Check if the current post has the category you want the title to be displayed with badges. Modify the function by adding has_category like so:

    function addBadge2Title($title)
    {
        // return the $title unmodified if the post does not have the category
        if(!has_category('badge-category'))
            return $title;
    
        // add the badge
        $seconds = strtotime("now") - strtotime(get_the_date("Y/m/d"));
        $badge = get_stylesheet_directory_uri() . '/library/images/new_ribbon.gif';
        if ($seconds < 10950400) {
            $title = '<img class="new_ribbon" width="75"  height="75" src="' . $badge . '" >' . $title;
        }
        return $title;
    }
    
2
  • Thanks for this @Aifrim but this applies all the titles across the site's posts and pages. I want to apply this to a specific category of blog post. Can we add a filer or condition to a specific blog category? thanks in advance for your time though
    – kalyan
    Sep 10 '14 at 7:26
  • @kalyan: I added the new code that fits your needs.
    – aifrim
    Sep 10 '14 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.