Question

I'm trying to add 'updated recently' after the post title. I've discovered some code which works but because of plugins and some theme coding it affects numerous instances of the_title that it shouldn't. This issue is covered here https://pippinsplugins.com/use-the_title-and-the_title_attribute-correctly/ - I've made some corrections / fixes but some plugins are a little beyond my capabilities.

The existing code (credit http://www.themesandco.com/snippet/adding-an-update-status-to-posts-in-wordpress/) is...

// add label to updated posts
add_filter('the_title' , 'skips_add_update_status');
function skips_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-default">Recently updated</span>' : '';

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

I'd like to adapt this to add an extra class 'update' using post_class and then I'll target the post title using CSS and add a pseudo element. My problem is I'm unsure how to adapt the code to insert...

$classes[] = 'update';
return $classes;

Presumably in place of the //adds html... onwards section of the existing code.

Can anyone assist?

This is my starting code but it adds 'update' class to all posts not just those updated in the last 14 days, I guess because there's no link to the logic calculation. If there is a better alternative approach I am open to ideas.

function skips_add_update_status($classes) {

//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;

//add update class
$classes[] = 'update';

return $classes;
}
add_filter('post_class' , 'skips_add_update_status');
Was it helpful?

Solution

You're right. The code you had didn't use any logic to determine whether to add the CSS class or not. Try this:

function skips_add_update_status($classes) {

    //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;

    // check whether $has_recent_update is true
    if( $has_recent_update ) {
        //add update class
        $classes[] = 'update';
    }

    return $classes;

}
add_filter('post_class' , 'skips_add_update_status');

I removed the check for whether it's ever been updated as you didn't seem to be using that.

To build on that, I'd argue that noting a recent post is more presentational than content. Therefore, you could do the whole thing with CSS. Using some made up classes because I don't know what your HTML looks like, it might be like this:

.update .post-title:before {
    content: "[Updated!]";
}

Depending on your styling requirements, you might want something a little more fancy:

.update .post-title:before {
    content: "[Updated!]";
    display: inline-block;
    padding: 2px;
    background-color: red;
    color: white;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top