- This topic has 10 replies, 2 voices, and was last updated 4 years, 1 month ago by
Elvin.
-
AuthorPosts
-
September 8, 2021 at 3:25 am #1922719
Tim
Hello,
A while back I contacted support via this forum to come up with a custom entry meta solution, and it is now exactly the way I want it (thank you). However, there is ONE thing I was hoping you might be able to help me with. I’d like to make the post date not display if the post is more than 24 months old. Including the CSS middle dot part.
Here’s a screenshot to show you what I mean:

So if the post is 24 months old or older, it would only show “By Tim * Product Reviews * Leave a comment”
Here is the code I am currently using…
PHP:
add_action( ‘wp’, function() {
if ( is_single() ) {
add_filter( ‘generate_show_comments’, ‘__return_true’ );
}
} );add_filter( ‘generate_post_author_output’, ‘tu_remove_by_byline’ );
function tu_remove_by_byline() {
printf( ‘ <span class=”byline”>%1$s</span>’, // WPCS: XSS ok, sanitization ok.
sprintf( ‘<span class=”author vcard” itemtype=”https://schema.org/Person” itemscope=”itemscope” itemprop=”author”>%1$s <span class=”author-name” itemprop=”name”>%4$s</span></span>’,
__( ‘By’, ‘generatepress’ ),
esc_url( get_author_posts_url( get_the_author_meta( ‘ID’ ) ) ),
/* translators: 1: Author name */
esc_attr( sprintf( __( ‘View all posts by %s’, ‘generatepress’ ), get_the_author() ) ),
esc_html( get_the_author() )
)
);
}add_filter( ‘generate_header_entry_meta_items’, function() {
return array(
‘date’,
‘author’,
‘categories’,
‘comments-link’,
);
} );add_filter( ‘generate_post_date_output’, db_modified_time_stamp, 20, 2);
function db_modified_time_stamp( $output, $time_string ) {
$time_string = ‘<time class=”entry-date published” datetime=”%1$s” itemprop=”datePublished”>Published on %2$s</time> ‘;if ( get_the_time( ‘U’ ) !== get_the_modified_time( ‘U’ ) ) {
$time_string = ‘<time class=”updated” datetime=”%3$s” itemprop=”dateModified”>Last Updated: %4$s</time> ‘ . $time_string;
}// get modified time and published time. Compare the two, If modified date exists then ouput both time strings with modified date
$updated_time = get_the_modified_time( ‘U’ );
$published_time = get_the_time( ‘U’ ) + 86400;if ( $updated_time > $published_time ) {
$time_string = ‘<time class=”entry-date updated-date” datetime=”%3$s” itemprop=”dateModified” style=”display:inline;”>Last Updated: %4$s</time>’;
}$time_string = sprintf( $time_string,
esc_attr( get_the_date( ‘c’ ) ),
esc_html( get_the_date(‘F j, Y’) ),
esc_attr( get_the_modified_date( ‘c’ ) ),
esc_html( get_the_modified_date(‘F j, Y’) )
);return sprintf( ‘<span class=”posted-on”>%1$s</span>’, // WPCS: XSS ok, sanitization ok.
sprintf( ‘%1$s’,
$time_string
)
);
}CSS:
.comments-link:before,
.cat-links:before {
content: “\25CF”;
margin: 0 10px;
color: #888888;
font-size: 10px;
}.posted-on:after {
content: “\25CF”;
margin: 0 10px;
color: #888888;
font-size: 10px;
}.entry-meta {
margin-top: 20px;
font-size: 14px;
}.entry-meta a:hover {
color: #24a2cc;
}.post-image-below-header.post-image-aligned-center .inside-article .featured-image {
margin-top: 20px !Important;
}—-
Also, I currently am using a plugin called Code Snippets to insert the code for this custom entry meta and I was wondering if it’d be possible to implement this using a GeneratePress custom hook or something instead? Doesn’t matter if not, but it’d be nice to not have to use another plugin is all.
Thank you for your help with this, it is very much appreciated.
Tim
September 8, 2021 at 3:51 am #1922740Tim
I should specify too… I’d like this to apply to posts that are 24 months old or older, and those that haven’t been edited within 24 months, please. So for example, if the post was published 23 months ago, or if it was last edited 23 months ago, the date will show. Thank you.
September 8, 2021 at 8:07 pm #1923653Elvin
StaffCustomer SupportHi Tim,
Try this one out.
add_filter( 'generate_post_date_output', db_modified_time_stamp, 20, 2); function db_modified_time_stamp( $output, $time_string ) { $time_string = <time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on %2$s</time> '; if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) { $time_string = <time class="updated" datetime="%3$s" itemprop="dateModified">Last Updated: %4$s</time> ' . $time_string; } // get modified time and published time. Compare the two, If modified date exists then ouput both time strings with modified date $updated_time = get_the_modified_time( 'U' ); $published_time = get_the_time( 'U' ) + 86400; if ( $updated_time > $published_time ) { $time_string = <time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified" style="display:inline;">Last Updated: %4$s</time>'; } $time_string = sprintf( $time_string, esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date('F j, Y') ), esc_attr( get_the_modified_date( 'c' ) ), esc_html( get_the_modified_date('F j, Y') ) ); $post_date = new DateTime(get_the_date( 'Y-m-d' )); $modified_date = new DateTime(get_the_modified_date( 'Y-m-d' )); $current_date = new DateTime(date('Y-m-d')); $published_date_interval = $post_date->diff($current_date); $modified_date_interval = $modified_date->diff($current_date); if ($published_date_interval->y < 2 || $modified_date_interval->y < 2 ){ return sprintf( <span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok. sprintf( '%1$s', $time_string ) ); } if ($published_date_interval->y >= 2 || $modified_date_interval->y >= 2 ) { return; } }September 9, 2021 at 5:33 pm #1924942Tim
Thanks Elvin, but that came up with a bunch of error messages when I inserted it, then broke my site when I submitted it. I can give you the login to the site if that helps?
September 9, 2021 at 8:05 pm #1924999Elvin
StaffCustomer SupportAh right, I missed the single quotes.
This one should be fine.
add_filter( 'generate_post_date_output', db_modified_time_stamp, 20, 2); function db_modified_time_stamp( $output, $time_string ) { $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on %2$s</time> '; if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) { $time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">Last Updated: %4$s</time> ' . $time_string; } // get modified time and published time. Compare the two, If modified date exists then ouput both time strings with modified date $updated_time = get_the_modified_time( 'U' ); $published_time = get_the_time( 'U' ) + 86400; if ( $updated_time > $published_time ) { $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified" style="display:inline;">Last Updated: %4$s</time>'; } $time_string = sprintf( $time_string, esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date('F j, Y') ), esc_attr( get_the_modified_date( 'c' ) ), esc_html( get_the_modified_date('F j, Y') ) ); $post_date = new DateTime(get_the_date( 'Y-m-d' )); $modified_date = new DateTime(get_the_modified_date( 'Y-m-d' )); $current_date = new DateTime(date('Y-m-d')); $published_date_interval = $post_date->diff($current_date); $modified_date_interval = $modified_date->diff($current_date); if ($published_date_interval->y < 2 || $modified_date_interval->y < 2 ){ return sprintf( '<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok. sprintf( '%1$s', $time_string ) ); } if ($published_date_interval->y >= 2 || $modified_date_interval->y >= 2 ) { return; } }Yes sure, you can provide the site details on the private information text field. 😀
September 11, 2021 at 2:51 am #1926557Tim
Thanks Elvin, that didn’t break my site but now it’s not outputting as it did in the above screenshot. I’ve added the login details so I’m not sure if it’s asking too much but if you wouldn’t mind taking a look I’d really appreciate it.
September 11, 2021 at 7:28 am #1926702Elvin
StaffCustomer SupportNot exactly sure what part are you pertaining to.
Is it the dot not being vertically centered? If that’s the case consider adding this CSS:
span.posted-on { display: inline-flex; }September 11, 2021 at 6:04 pm #1927224Tim
I just mean the entry meta doesn’t display as it did with the old code.
Before (original code):

After (your code):

I want it to display exactly as it did before, I just don’t want the date part to show if the post is 24 months old or older. Or if it hasn’t been updated in more than 24 months.
September 11, 2021 at 8:15 pm #1927275Elvin
StaffCustomer SupportAh I see what seems to be the issue here.
The code I’ve provided was meant to replace only the part that used
generate_post_date_outputfilter.You should still keep the other parts which are the PHP snippets using the filters
generate_post_author_output,generate_header_entry_meta_itemsand this one:add_action( 'wp', function() { if ( is_single() ) { add_filter( 'generate_show_comments', '__return_true' ); } } );September 12, 2021 at 1:58 am #1927467Tim
Brilliant that worked, thank you so much Elvin! Really appreciate your time with all of this.
September 12, 2021 at 2:19 am #1927498Elvin
StaffCustomer SupportNo problem. Glad to be of any help. 😀
-
AuthorPosts
- You must be logged in to reply to this topic.