0

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');
1

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;
}
  • thank you that seems to have done the trick with one amendment. Having removed the ever updated check $has_been_updated && needs removing from the code also I think (at least that's what I did). If you'd like to edit your response I can mark it as accepted. I was unsure about the bit you deleted - I'm thinking I may keep it in and change the values from '0' to allow a short period of editing after posting without it being classes as an update - (I always spot a mistake). Agree about this being a CSS issue ideally. Thank you. – Paul Brown Aug 25 '14 at 18:28
  • Actually I don't think that's what has_been_updated is doing so I can't see it serves a purpose unless a new post automatically inherits an updated status at the same time it is first published (which I assume it doesn't)? – Paul Brown Aug 25 '14 at 18:43
  • 1
    (Code above fixed) From the_modified_date() (the louder sibling of get_the_modified_date()): "If the post or page is not yet modified, the modified date is the same as the creation date." So a page's modified date is either it's most recent update OR its publication date. I think that aligns with what you're trying to do. – mrwweb Aug 25 '14 at 18:52
  • Okay thank you. In that case I do think the bit stripped out needs retaining - otherwise newly published posts will show an 'updated' icon (will use Bootstrap I think) if I understand you correctly. I only want it on updated existing posts. Even so the answer is fine and resolves my issue. Much appreciated. – Paul Brown Aug 25 '14 at 18:57
  • Gotcha. Code updated for future answer-seekers :) Now only shows for posts that are newly updated but not just posted. – mrwweb Aug 25 '14 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.