- This topic has 6 replies, 3 voices, and was last updated 3 years ago by
Ying.
-
AuthorPosts
-
March 15, 2023 at 6:22 am #2568485
roninway
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 );March 15, 2023 at 6:58 am #2568532David
StaffCustomer SupportHi there,
Whenever a code has
add_actionoradd_filterit 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_actioncode for you.
They can be used for inserting content into the template, but they cant be used to filter existing content.March 15, 2023 at 9:30 am #2568810roninway
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.
March 15, 2023 at 9:40 am #2568830roninway
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.
March 15, 2023 at 9:54 am #2568856Ying
StaffCustomer SupportI 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.
March 15, 2023 at 10:56 am #2568932roninway
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!
March 15, 2023 at 3:28 pm #2569157Ying
StaffCustomer SupportIf 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/
-
AuthorPosts
- You must be logged in to reply to this topic.