Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Display Last Modified Date On PAGES

  • Looked around the Internet on the above and almost everything I see refers to POSTS.

    On a site we have, we turn WP on its head and favour PAGES for content over posts that to my mind belong to a sideshow – a blog.

    Works great for evergreen content but would like to display last modified date without using a plugin.

    On the page source I can see the author and last modified date etc. schema put there by WP itself or The SEO Framework. Currently using the following to display last modified date on POSTS:

    `$u_time = get_the_time( ‘U’ );
    $u_modified_time = get_the_modified_time( ‘U’ );
    // Only display modified date if 24hrs have passed since the post was published.
    if ( $u_modified_time >= $u_time + 86400 ) {

    $updated_date = get_the_modified_time( ‘F jS, Y’ );
    $updated_time = get_the_modified_time( ‘h:i a’ );

    $updated = ‘<p class=”last-updated”>’;

    $updated .= sprintf(
    // Translators: Placeholders get replaced with the date and time when the post was modified.
    esc_html__( ‘Last updated on %1$s at %2$s’ ),
    $updated_date,
    $updated_time
    );
    $updated .= ‘</p>’;

    echo wp_kses_post( $updated );
    }

  • Hi there,

    You should be able to use the same code to add the last updated date to the pages.

    You can use a hook element, add the code you are using for the posts, and tick the Execute PHP box.

  • Hmmmm…playing around with it, it prints the code on the page. Using generate_before_content and also “after”.

  • Hi there,

    see this doc here, it provides a function for that:

    https://docs.generatepress.com/article/add-entry-meta-to-pages/

  • Still no luck David. I’ve added the Add Entry Meta to Pages code via Code Snippets and this does not display Author or Date on pages.

    If I then go and try to add the code via an element like Ying suggested, the code is printed out in the web page itself.

  • If I then go and try to add the code via an element like Ying suggested, the code is printed out in the web page itself.

    If you use your original code, you need to add <?beore the code and ?> after the code to tell the hook element that it’s a PHP code not HTML, and you need to make sure the Execute PHP box is ticked in the element.

    <?
    $u_time = get_the_time('U');
    $u_modified_time = get_the_modified_time('U');
    
    // Only display modified date if 24hrs have passed since the post was published.
    if ($u_modified_time >= $u_time + 86400) {
        $updated_date = get_the_modified_time('F jS, Y');
        $updated_time = get_the_modified_time('h:i a');
        
        $updated = '<p class="last-updated">';
        
        $updated .= sprintf(
            // Translators: Placeholders get replaced with the date and time when the post was modified.
            esc_html__('Last updated on %1$s at %2$s'),
            $updated_date,
            $updated_time
        );
        
        $updated .= '</p>';
        
        echo wp_kses_post($updated);
    }
    ?>
  • Well, I have it working on my test site. But on my main site it is printing out the code and I have double-checked it is the same as the test site cleared cache etc.. I will leave it on the main site for a little while before turning it off.

  • I’ve cloned the website and have it running with the code to show the error.

    It is running well on a separate URL, but not on the main site.

  • In your Hook Element do you have the Execute PHP option checked ?

  • Experimenting, I discovered that adding <?php instead of just <?

    at the start of the code makes it work. Reference: https://www.php.net/manual/en/language.basic-syntax.phptags.php

    But the other test site running good the change is not needed.

    Should I proceed and deploy on the live site you think? Would future updates break something?

    Observations:

    1. On the separate URL I provided, the code as provided by Ying runs fine. GPP element hook executes the php properly.

    2. On the staging URL I need to add the php opening tag as described above otherwise GPP will not execute the php.

    3. If I remove the closing tag, ?> the site crashes. I think GPP element hook should be adjusted somewhat like WPCODE so that this does not occur. With WPCODE when you select PHP it automatically adds <?php at the start.

  • Correct – Line 1 needs to be : <?php and you should close the function too ?>
    If it works on the other site then its probably an unclosed function elsewhere

    GP Hooks don’t make any stipulation as to what code is added to it. So we can’t auto add these things as a user may not want to require them. To add them would be us guessing that you need them

  • I just visited your test site, it seems working fine:https://www.screencast.com/t/cQZ8E1Cc9y

  • Yes thanks it is working fine. I think this thread will become a reference in the search engines.

  • Glad to hear that 🙂

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