Site logo

Archived topic

Posted Date and Updated Date

11 replies · Started by Matt on January 9, 2019

Viewing posts 1–12 of 12

I know this question has been asked before, but I just can't figure out the best way to do it. If someone could take a look at my site and tell me what to do I'd really appreciate it.

I'd like to have the date appear as follows:

Posted Date: XXXXXX Updated Date: XXXXXX

I added my URL for the admins to see.

Thanks for the help!

Hi there,

Add this PHP snippet:

add_filter( 'generate_post_date_output', function( $output ) {
	$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_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() )
	);

	echo sprintf( '<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok.
		sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
			esc_url( get_permalink() ),
			esc_attr( get_the_time() ),
			$time_string
		)
	);
} );

Adding PHP: https://docs.generatepress.com/article/adding-php/

And this CSS snippet:

.posted-on .updated {
    display: block;
}
.posted-on .updated + .entry-date {
    display: none;
}
.posted-on .updated:before {
    content: " Updated Date: ";
}
.posted-on .published:before {
    content: "Posted Date: ";
}

Adding CSS: https://docs.generatepress.com/article/adding-css/

We are working on better methods to make this easier :)

Thanks, I add the PHP and CSS, but it doesn't look right.

Can you please take a look at one of my blog post pages - there are some spaces missing.

Also, I would prefer to have the Last Updated date appear a line below the Posted date.

I also would like everything to appear below the featured image I think :)

Thanks for the help.

Updated the CSS above.

I'm still seeing the old CSS being added.

Have you tried clearing your caching plugin?

Yes, even cleared Cloudflare.

Opps, forgot to delete what was there when I moved it over to the Simple CSS plugin :)

Almost done.

I would prefer to not display the date on tag or category pages:

https://www.yardcare.life/tag/buying-guide/

I think it looks funny.

Got it, thank you!

No problem :)

This archived topic is closed to new replies.