Site logo

[Support request] Generate Press Setting for Article Post Date and Revision Date

Home Forums Support [Support request] Generate Press Setting for Article Post Date and Revision Date

Home Forums Support Generate Press Setting for Article Post Date and Revision Date

  • This topic has 6 replies, 3 voices, and was last updated 3 years ago by Ying.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #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 );

    #2568532
    David
    Staff
    Customer Support

    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.

    #2568810
    roninway

    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.

    #2568830
    roninway

    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.

    #2568856
    Ying
    Staff
    Customer Support

    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.

    #2568932
    roninway

    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!

    #2569157
    Ying
    Staff
    Customer Support

    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/

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.