Adding a NEW label for Posts published in the last x number of days in Genesis

Posted on
5/5 - (184 votes)

In Genesis Slack chat, a user asked:

I am trying to use the Custom Featured Post plugin to add a NEW label before the title. Has anyone tried this before? I am thinking of just accessing the [post_date] and checking if it is > 90 or >180 days or something.

In this tutorial, I show how [NEW] text can be added before the title markup for posts in all the various views like archives and single as well as in the output of Genesis Featured Posts widget.

We shall make use of genesis_post_title_output filter hook and Custom Genesis Featured Posts Widget plugin.

Note: The above screenshot was taken when 1 was used instead of 90 during testing in the code below.

Step 1

Add the following in child theme’s functions.php:

/**
 * Function to check if a post is older than certain days
 *
 * @param integer $days Number of days.
 * @return True if post is older than the supplied number of days, otherwise false
 * @link http://wpengineer.com/1913/if-post-is-older-than/
 */
function is_old_post( $days = 5 ) {
    $days = (int) $days;
    $offset = $days * 24 * 60 * 60;
    if ( get_post_time() < date( 'U' ) - $offset ) {
        return true;
    }

    return false;
}

add_filter( 'genesis_post_title_output', 'custom_post_title_output' );
/**
 * Filter Genesis Post Titles to add a [NEW] label for posts that have been published within the last 90 days.
 *
 * @param string $output Current title output markup.
 */
function custom_post_title_output( $output ) {

    // if this is a Post and is NOT older than 90 days i.e., was published in the last 90 days.
    if ( 'post' === get_post_type() && ! is_old_post( 90 ) ) {
        $output = '<span class="new alignleft">[NEW]</span>' . $output;
    }

    return $output;

}

Step 2

Install and activate this customized Custom Genesis Featured Posts Widget plugin: http://d.pr/f/r5GB

Step 3

Add the following in child theme’s style.css:

.new {
    margin-right: 8px;
    font-size: 15px;
}