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
-
First remove
add_filter('the_title', 'addBadge2Title');
and keep only thefunction
declaration infunctiosn.php
and in thearchive-{$category-slug}.php
file add and remove the filter like soif(have_posts()) : while(have_posts()) : the_post() add_filter('the_title', 'addBadge2Title'); the_title(); remove_filter('the_title', 'addBadge2Title'); endwhile; endif;
-
Check if the current post has the category you want the title to be displayed with badges. Modify the
function
by addinghas_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; }