Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Add "Recently Updated" label next to post titles WordPress

  • SOLVED

I found the code below that I can insert into my functions.php. It displays a nice "Recently Updated" label next to <strong>all</strong> the post titles on my website.

The trick is, I do NOT want the recently updated label next to EVERY post title... and I'd also like to place a string of code manually into each template file to specify where the label should show.

Is there a way I can tweak the following to work like I need? And possibly create a call to it from my templates?

// Add's recently updated label
add_filter('the_title' , 'add_update_status');
function add_update_status($html) {
//First checks if we are in the loop and we are not displaying a page
if ( ! in_the_loop() || is_page() )
return $html;

//Instantiates the different date objects
$created = new DateTime( get_the_date('Y-m-d g:i:s') );
$updated = new DateTime( get_the_modified_date('Y-m-d g:i:s') );
$current = new DateTime( date('Y-m-d g:i:s') );

//Creates the date_diff objects from dates
$created_to_updated = date_diff($created , $updated);
$updated_to_today = date_diff($updated, $current);

//Checks if the post has been updated since its creation
$has_been_updated = ( $created_to_updated -> s > 0 || $created_to_updated -> i > 0 ) ? true : false;

//Checks if the last update is less than n days old. (replace n by your own value)
$has_recent_update = ( $has_been_updated && $updated_to_today -> days < 14 ) ? true : false;

//Adds HTML after the title
$recent_update = $has_recent_update ? '<span class="label label-warning">Recently updated</span>' : '';

//Returns the modified title
return $html.'&nbsp;'.$recent_update;
}

Answers (3)

2014-09-04

Fahad Murtaza answers:

Keep the function as it is. Just remove this line from top

add_filter('the_title' , 'add_update_status');

and call the function from the template where you have title, like following

add_update_status($get_the_title());

So instead of the code which shows title, just pass the post title as parameter to this and get the label attached to it from function. Removing the first line will remove the auto filter and will allow you to call it manually.


streetfire comments:

Yay Fahad! I was able to make some minor tweaks and got this to work!

I removed the first filter like you said. ( I thought he was the culprit ) and also removed $html from "the argument" ?

Then I took some strategy from Remy's code and used "echo add_update_status();" to place the text next to the title.

:) :) Thanks!

2014-09-04

Arnav Joy answers:

You can add a custom field to the post/page , and can use it to show/hide

then you can use following code to show it in desired post

Note:- you have to named custom field as :- "show_recently_updated" and its value should be set to "1"

<?php


add_filter('the_title' , 'add_update_status');

function add_update_status($html) {

//First checks if we are in the loop and we are not displaying a page

if ( ! in_the_loop() || is_page() )

return $html;



//Instantiates the different date objects

$show_recently_updated = get_post_meta( get_the_ID() , 'show_recently_updated' , true );

if( $show_recently_updated != 1 )
return $html;

$created = new DateTime( get_the_date('Y-m-d g:i:s') );

$updated = new DateTime( get_the_modified_date('Y-m-d g:i:s') );

$current = new DateTime( date('Y-m-d g:i:s') );



//Creates the date_diff objects from dates

$created_to_updated = date_diff($created , $updated);

$updated_to_today = date_diff($updated, $current);



//Checks if the post has been updated since its creation

$has_been_updated = ( $created_to_updated -> s > 0 || $created_to_updated -> i > 0 ) ? true : false;



//Checks if the last update is less than n days old. (replace n by your own value)

$has_recent_update = ( $has_been_updated && $updated_to_today -> days < 14 ) ? true : false;



//Adds HTML after the title

$recent_update = $has_recent_update ? '<span class="label label-warning">Recently updated</span>' : '';



//Returns the modified title

return $html.'&nbsp;'.$recent_update;

}



Let me know you need any help ..


Arnav Joy comments:

Please visit this link to see how you can create custom field
http://codex.wordpress.org/Custom_Fields

2014-09-04

Remy answers:

How old are your posts ? It's supposed to show up only for post updated in the last 2 weeks.

You can use this code as a base to add it in your template, add this function in your functions.php file

function rp_add_update_status() {
//First checks if we are in the loop and we are not displaying a page
if ( !is_single() )
return;
//Instantiates the different date objects
$created = new DateTime( get_the_date('Y-m-d g:i:s') );
$updated = new DateTime( get_the_modified_date('Y-m-d g:i:s') );
$current = new DateTime( date('Y-m-d g:i:s') );
//Creates the date_diff objects from dates
$created_to_updated = date_diff($created , $updated);
$updated_to_today = date_diff($updated, $current);
//Checks if the post has been updated since its creation
$has_been_updated = ( $created_to_updated -> s > 0 || $created_to_updated -> i > 0 ) ? true : false;
//Checks if the last update is less than n days old. (replace n by your own value)
$has_recent_update = ( $has_been_updated && $updated_to_today -> days < 14 ) ? true : false;
//Adds HTML after the title
$recent_update = $has_recent_update ? '<span class="label label-warning">Recently updated</span>' : '';
//Returns the modified title
return $recent_update;
}


then you call it in your template where you wan to display the "Recently updated" text


echo rp_add_update_status();


streetfire comments:

Hi! This works, but only on the single post page (single.php). I mainly would like it to work on an archive page (archive-mycustomposttype.php). Is that doable?