Site logo

Archived topic

Post meta with last updated date

12 replies · Started by Matthew on April 15, 2020

Viewing posts 1–13 of 13

I use the code from below resource:

https://generatepress.com/forums/topic/read-more-button-and-author-gravatar/#post-623023

also shown below

add_filter( 'generate_post_author', '__return_false' );
add_filter( 'generate_show_comments', '__return_false' );
add_filter( 'generate_post_date_output', 'tu_fancy_byline' );
function tu_fancy_byline( $date ) {
	printf( ' <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' ) )
		)
	);

	if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
		echo '<span class="comments-link">';
			comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
		echo '</span>';
	}

	echo $date;
}

looks like:

https://drive.google.com/open?id=1diZVX-EMeYJPULfpUT1jJhvMF-F9THTG

1 problem how can I make the date the last modified date as in below:

https://drive.google.com/open?id=1k0Mc-0sLyIamXfJgpACtHK_cHFKENpLZ

I don't want the published date anywhere, just the updated date.

I find below resource:

https://generatepress.com/forums/topic/please-help-me/

and adjust the earlier code to the code below by replacing echo $date;

add_filter( 'generate_post_author', '__return_false' );
add_filter( 'generate_show_comments', '__return_false' );
add_filter( 'generate_post_date_output', 'tu_fancy_byline' );
function tu_fancy_byline( $date ) {
	printf( ' <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' ) )
		)
	);

	if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
		echo '<span class="comments-link">';
			comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
		echo '</span>';
	}

	$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
	
	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
		$time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">%4$s</time>';
	}
	
	$time_string = sprintf( $time_string,
		esc_attr( get_the_date( 'c' ) ),
		esc_html( get_the_date() ),
		esc_attr( get_the_modified_date( 'c' ) ),
		esc_html( get_the_modified_date() )
	);

	return sprintf( '<span class="posted-on">%s</span>', // WPCS: XSS ok, sanitization ok.
		$time_string
	);
}

But now the date disappears completely, as shown below:

https://drive.google.com/open?id=1QZ-UoDGZLuWwbbgTBbKDjgdcZvUdY5Tt

thanks,

Hi there,

Try this instead:

add_filter( 'generate_post_author', '__return_false' );
add_filter( 'generate_show_comments', '__return_false' );
add_filter( 'generate_post_date_output', 'tu_fancy_byline' );
function tu_fancy_byline( $date ) {
	printf( ' <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' ) )
		)
	);

	if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
		echo '<span class="comments-link">';
			comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
		echo '</span>';
	}

	$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
	
	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
		$time_string = '<time class="updated-date" datetime="%3$s" itemprop="dateModified">%4$s</time>';
	}
	
	$time_string = sprintf( $time_string,
		esc_attr( get_the_date( 'c' ) ),
		esc_html( get_the_date() ),
		esc_attr( get_the_modified_date( 'c' ) ),
		esc_html( get_the_modified_date() )
	);

	printf( '<span class="posted-on">%s</span>', // WPCS: XSS ok, sanitization ok.
		$time_string
	);
}

Let me know :)

Hi Tom,

I added a new testing line of text to my post and updated the post (after inserting your suggested code) but the date is still missing like the final image I uploaded on google drive above.

I added your code to my site-specific plug-in, i.e.

/public_html/wp-content/plugins/imjexa-plugin

thanks,

Any chance you can link me to the post? I'm not able to get through the password protected page right now.

Thanks Tom it works,

How can I refine this so that for a newly published post it displays as:

Matthew Jones | 2 comments | Published on April 20, 2020

and for any updated post it is:

Matthew Jones | 2 comments | Last updated on April 20, 2020

Currently I have:

Matthew Jones | 2 comments | April 20, 2020

thanks,

note: the comments part is only shown on the archive page which is OK, it's not on the single posts page which is how I want it.

Hi there,

for the Published and Last Updated dates - you will need to edit the two lines beginning with $time_string

Published date should read like so:

$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on %2$s</time>';

and this for the Updated:

$time_string = '<time class="updated-date" datetime="%3$s" itemprop="dateModified">Last Updated on %4$s</time>';

I just published a new test post and on the single post it is displaying as:

Matthew Jones | Last updated on April 21, 2020

and on the archive:

Matthew Jones | 2 comments | Last updated on April 21, 2020

I put my login earlier, is it possible to test this on your end to check it's OK first?

thanks,

Can you try this instead:

add_filter( 'generate_post_author', '__return_false' );
add_filter( 'generate_show_comments', '__return_false' );
add_filter( 'generate_post_date_output', 'tu_fancy_byline' );
function tu_fancy_byline( $date ) {
	printf( ' <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' ) )
		)
	);

	if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
		echo '<span class="comments-link">';
			comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
		echo '</span>';
	}

    $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on %2$s</time>';
    
    $updated_time = get_the_modified_time( 'U' );
    $published_time = get_the_time( 'U' ) + 86400;
	
	if ( $updated_time > $published_time ) {
		$time_string = '<time class="updated-date" datetime="%3$s" itemprop="dateModified">Last Updated on %4$s</time>';
	}
	
	$time_string = sprintf( $time_string,
		esc_attr( get_the_date( 'c' ) ),
		esc_html( get_the_date() ),
		esc_attr( get_the_modified_date( 'c' ) ),
		esc_html( get_the_modified_date() )
	);

	printf( '<span class="posted-on">%s</span>', // WPCS: XSS ok, sanitization ok.
		$time_string
	);
}

Na doesn't work as expected, just comes back to:

Matthew Jones | April 21, 2020

and on the archive:

Matthew Jones | 2 comments | April 21, 2020

Don't worry about this for now, I'm going to try another approach, and re-post.

thanks,

I updated the code here as i forgot to add in the labels.

Give that a go and let us know.

This archived topic is closed to new replies.