Site logo

Archived topic

Displaying Posted (and updated) Date on pages

25 replies · Started by culpable on December 28, 2018

Viewing posts 16–26 of 26

#2/#3 There are two ways to use hooks, one is to use Hook Element and the other is to use functions:
https://docs.generatepress.com/article/using-hooks/

It's generally easier to use hook element if you aren't comfortable with coding as you can set it to show on certain pages without using PHP conditional tags.

The difference in performance should be minimal. Code is code no matter how you add them :)

Thanks Leo, that helps a lot. On your point about the hook element's ability to show on certain pages without code – does that mean that if I use the display rule "All Pages", then I can omit the "php if" part of the below:

<?php if ( 'page' == get_post_type() ) : ?>
	<div class="entry-meta">
		<?php generate_posted_on(); ?>
	</div>
<?php endif; ?>

Also – in what location should I place this hook (before_main_content)?

And lastly – I presume that I tick the "Execute PHP" button? :)

Yes! It's working now :)

Is there a way to reverse the order in which it shows the dates (i.e. display "Published ____ & Last updated ____")?

It's currently showing: https://imgur.com/a/Voskp7n

And I'm using the following CSS code along with your hook above:


.posted-on .updated {
    display: inline-block;
}
.posted-on .updated:before {
    content: "Last updated ";
}
.posted-on .updated:after{
    content: " &"
}
.posted-on .published:before {
    content: " Published ";
}

Thank you for your time Leo :)

Any chance you can link me to the page in question?

Sure thing Leo – image and link here

I tried adding the code in the same hook as your previous suggestion:

<div class="entry-meta"><?php generate_posted_on(); ?></div>
<?php
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
		)
	);
} );
?>

But it doesn't seem to be working. Did I enter it in wrong? (highly likely, sorry Leo)

Also Leo as per this link, I just noticed my author pages aren't showing anything.

I'm guessing this is because I have no posts, only pages. Is there code I can add to fix this? If so, can you show me how I have to wrap PHP code such that it executes within the Elements hook – would this require a display rule on Author archive pages?

Otherwise, if it's easier I can add the code to display the authored pages as a PHP snippet.

Working!!!

Since the author question is separate, I'll make a new post for that.

Thank you very much for your time Leo.

No problem :)

This archived topic is closed to new replies.