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;
}
References
genesis/lib/structure/post.php
https://sridharkatakam.com/remove-post-info-posts-older-30-days-genesis/
https://sridharkatakam.com/custom-featured-post-widget-plugin/
Hey Sridhar,
For titles that are centered, is there any way to move the NEW text without impacting the positioning of the Title?
Do you have only some posts’ titles centered in your site? And, are you looking to have the NEW label accompany the title i.e., be adjacent to it?
Nope, all post titles are centered like this(https://staging.avina.sh/big-boy-pants/).
Also, what I’m looking do to is have the NEW label act as a kind of superscript acting as a kind of block element. But what’s currently happening is any styling of the NEW label also impacts the positioning of the post title.
[…] Adding a NEW label for Posts published in the last x number of days in Genesis […]
your plugin broke my site, is there any step to skip uploading the plugin ? thankyou