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

Make function also show Icon for Newly Added Posts WordPress

  • SOLVED

Currently I have the following function that displays an icon when a post has been updated within the last 7 days. I would like this icon to also show when a post has been added within the last 7 days. Is there a minor tweak that can be made to this function to allow that?

<strong>Function
</strong>
// Create's recently updated label (for attaching to templates. Use echo add_update_status(); to retrieve value

function add_update_status() {

//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 < 7 ) ? true : false;

//Adds HTML after the title

$recent_update = $has_recent_update ? '<small><a href="#" data-toggle="tooltip" title="Updated: '. get_the_modified_date('M j') .' at '. get_the_modified_date('g:i a') .'"><span class="glyphicon glyphicon-asterisk text-orange"></span></a></small>' : '';


//Returns the modified title

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

}


<strong>Call from Template:</strong>

<?php echo add_update_status(); ?>

Answers (1)

2014-10-15

Arnav Joy answers:

try this

<?php

// Create's recently updated label (for attaching to templates. Use echo add_update_status(); to retrieve value



function add_update_status() {



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

$created_to = date_diff($created, $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 < 7 ) ? true : false;

$has_created = ( $created_to -> days < 7 ) ? true : false;



//Adds HTML after the title



$recent_update = $has_recent_update ? '<small><a href="#" data-toggle="tooltip" title="Updated: '. get_the_modified_date('M j') .' at '. get_the_modified_date('g:i a') .'"><span class="glyphicon glyphicon-asterisk text-orange"></span></a></small>' : '';

if( $has_created ) {
$recent_update = '<small><a href="#" data-toggle="tooltip" title="Updated: '. get_the_modified_date('M j') .' at '. get_the_modified_date('g:i a') .'"><span class="glyphicon glyphicon-asterisk text-orange"></span></a></small>';
}





//Returns the modified title



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



}


streetfire comments:

Hi, thank you! That works great!