WordPress.org

Forums

Plugin to add a "new badge" to title (3 posts)

  1. georgek86
    Member
    Posted 3 years ago #

    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;
    		}
    }
  2. yehudah
    Member
    Posted 3 years ago #

    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. '';})
  3. alrobelller
    Member
    Posted 3 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.