Archived topic
Updated Post Date (but with a customization)
5 replies · Started by Milo Lam on July 29, 2021
Hi,
I have seen quite a few of topics on Showing Updated Posts up here but I wasn't sure how to apply it with custom codes that were already there by a previous developer. He's no longer with us so I am taking it into my own hands.
Specifically, this is a custom snipper I currently have right now for my blog category pages:
add_filter( 'generate_post_author_output', function($output) {
if( is_archive() ){
$output = sprintf( ' <span class="byline">%1$s</span>',
sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%4$s<a href="%1$s" title="%2$s" rel="author"><span class="author-name" itemprop="name">%3$s</span></a></span>',
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
esc_html( get_the_author() ),
get_avatar( get_the_author_meta( 'ID' ) )
)
);
}
return $output;
} );
add_filter( 'generate_header_entry_meta_items', function( $items ) {
if( is_single() ){
return array(
//'categories',
'date'
);
}
return $items;
} );
add_filter( 'generate_footer_entry_meta_items', function( $items ) {
if( is_archive() ){
return array(
'author',
'comments-link'
);
}
return $items;
} );
add_filter( 'generate_dynamic_element_text', function($post_date, $block){
if( (is_archive() || (!is_front_page() && is_home())) && $block['attrs']['gpDynamicTextType'] === 'post-date' ){
$post_date = sprintf(
'<time class="entry-date published" datetime="%1$s"><span class="entry-date">%2$s</span><span class="entry-month">%3$s</span></time>',
esc_attr( get_the_date( 'c', $id ) ),
esc_html( get_the_date( 'd', $id ) ),
esc_html( get_the_date( 'M', $id ) )
);
return $post_date;
}
return $post_date;
},15,2);
add_filter( 'generate_post_date_output', function($post_date){
if( is_single() ){
$post_date = sprintf(
'<time class="entry-date published" datetime="%1$s"><span class="entry-date">%2$s</span><span class="entry-month">%3$s</span></time>',
esc_attr( get_the_date( 'c', $id ) ),
esc_html( get_the_date( 'd', $id ) ),
esc_html( get_the_date( 'M', $id ) )
);
return $post_date;
}
return $post_date;
},15,2);
How would I change it so that the output is modified/updated date (only when there is an update date available)?
Thank you!
Hi there,
You can actually use a block element with dynamic content without any custom coding now:
https://docs.generatepress.com/article/block-element-post-meta-template/
Hope this helps :)
Hi,
Sorry I forgot to mention. The previous developer had in a ton of CSS to pair with the above snippet already so I was wondering how to go about showing updated date without losing the style on it like this.
Not sure if I fully understand.
Are you talking about the date element here?
https://www.screencast.com/t/8ggAC5Zp6g
It's already using GenerateBlocks and not the snippet.
yes, that's the one.
Huh, that's strange. Because when I disable the snippet, it loses the CSS styles. Do you mind inspecting how I can correct the CSS so that it applies to updated date? I see that on GB it is set to show updated date when available but what appears is always the published date :/
Thank you
I can't really help with the customization done by a previous developer unfortunately.
I'm seeing a conditional tag here which is kind of weird:
if( (is_archive() || (!is_front_page() && is_home())) && $block['attrs']['gpDynamicTextType'] === 'post-date' ){
Not really sure why that's needed.
Can you try contacting your developer and see if he/she do the changes you need for you?
Or convert everything to blocks?