Support » Fixing WordPress » Plugin to add a "new badge" to title

  • Hi I am trying to make a new plugin that will include a “new badge” to the title of each post less than 24 hours old.

    I have managed to include the badge in the content of the post but I am facing an issue using the post’s the_title hook and that’s because the_title hook is being used and in other places inside the code. What can I do to make it appear only at the post’s title? Is there any other way besides messing with my template?

    My working code is below but shows the badge in the content and not in the title.

    add_filter('the_content', 'addBadge2Title');
    function addBadge2Title($content = ''){
    		$seconds=strtotime("now")-strtotime(get_the_date("Y/m/d"));
    		if( $seconds < 86400) {
    			echo '<img width="32"  height="32" src="./wp-content/plugins/addBadge2Title/images/label_new_red.png" style="border:none;">'.$content;
    		} else {
    			echo $content;
    		}
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • hey

    didn’t test it but hope you got the idea

    add_filter('the_title', function($title) { return '<img width="32"  height="32" src="./wp-content/plugins/addBadge2Title/images/label_new_red.png" style="border:none;">'. $title. '';})
    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.'" >';
    }
    }

    more elegant solution
    relative path to image
    on paged thumbnails list the path was broken

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Plugin to add a "new badge" to title’ is closed to new replies.