Site logo

Archived topic

Generate Press Setting for Article Post Date and Revision Date

6 replies · Started by roninway on March 15, 2023

Viewing posts 1–7 of 7

I will admit, I am not a tech overlord so spend time working on themes with forum support and Youtube. I may be wrong but am a little surprised that a simple setting like "posted date, revision date" for all blog articles isn't a checkbox or something. Correct me if am I wrong.

But basically, I used a "snippets" plugin and want to remove this because I realize Generate Press has hooks.

How do I actually configure this PHP code for a hook that displays this on my site for all articles/posts on the home page, and within the body of the article. If there are any exclusions to consider, please let me know.

Asking for a bit of direction because this is really foreign to me. So I need to know which menu prompt to use when I create the hook > wp_head (or whatever) > display rules.

add_filter( 'generate_post_date_output', function( $output, $time_string ) {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';

if ( get_the_date() !== get_the_modified_date() ) {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on: %2$s</time> | <time class="entry-date 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() )
);

return sprintf( '<span class="posted-on">%s</span> ',
$time_string
);
}, 10, 2 );

Hi there,

Whenever a code has add_action or add_filter it needs to be added using the Code Snippets plugin or in a Child Theme functions.php. So that code has to remain where it is.

GP Elements use action hooks, they effectively write the add_action code for you.
They can be used for inserting content into the template, but they cant be used to filter existing content.

Understood.

So to save myself another plugin, I can add the above into the child theme's function.php at the end? I am using Ezoic and it's freaking out over the code snippets plugin.

Got a syntax error with the above.

Asked ChatGPT what to do and gave me this:
<?php
$published_date = get_the_date();
$modified_date = get_the_modified_date();
?>
<p>Published: <?php echo $published_date; ?></p>
<p>Modified: <?php echo $modified_date; ?></p>

Got syntax error for it too. LMAO.

I can add the above into the child theme’s function.php at the end?

Yes.

You can paste the code to chatGPT and let it find the syntax error for you.

The code chatGPT provided is not a filter, it can not be used to filter existing content, but you can use it in a hook element.

It's not able to diagnose.

Can you recommend PHP code which provides published and modified date information underneath the headline of the article and under the image/article headline on the home page?

Thanks!

If you are not familiar with code, the easiest option would be using a block element - post meta template, then you can set it to display the published date initially, and if there's an updated date, then display the updated date instead.

The above solution will not require any codes, and it comes with some built-in templates for a quick start.

For more info: https://docs.generatepress.com/article/block-element-post-meta-template/

This archived topic is closed to new replies.