Site logo

[Support request] Customising Entry Meta (Published vs Updated)

Home Forums Support [Support request] Customising Entry Meta (Published vs Updated)

Home Forums Support Customising Entry Meta (Published vs Updated)

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1742584
    Tim

    Hello. I have searched high and low throughout the support articles and managed to implement a solution to customising the entry meta on my site fairly well, but there’s one last thing I can’t seem to crack.

    Basically, everything on the following two pages is correct except that on the first link (post that has NOT been updated) I would like to show the publish date, not the date it was updated.

    Example Post (Not Updated): https://waclasses.siterubix.com/example-post-not-updated/

    Example Post (Updated): https://waclasses.siterubix.com/example-of-an-updated-post/

    I have shown the publish/update dates in the post to make it more clear what I mean. Also, so you know what I’ve done so far, I have:

    1) Added the following two (separate) custom PHP code snippets:

    add_action( ‘wp’, function() {
    if ( is_single() ) {
    add_filter( ‘generate_show_comments’, ‘__return_true’ );
    }
    } );

    And…

    add_filter( ‘generate_post_author_output’, ‘tu_remove_by_byline’ );
    function tu_remove_by_byline() {
    printf( ‘ <span class=”byline”>%1$s</span>’, // WPCS: XSS ok, sanitization ok.
    sprintf( ‘<span class=”author vcard” itemtype=”https://schema.org/Person&#8221; itemscope=”itemscope” itemprop=”author”>%1$s <span class=”author-name” itemprop=”name”>%4$s</span></span>’,
    __( ‘By’, ‘generatepress’ ),
    esc_url( get_author_posts_url( get_the_author_meta( ‘ID’ ) ) ),
    /* translators: 1: Author name */
    esc_attr( sprintf( __( ‘View all posts by %s’, ‘generatepress’ ), get_the_author() ) ),
    esc_html( get_the_author() )
    )
    );
    }

    add_filter( ‘generate_header_entry_meta_items’, function() {
    return array(
    ‘date’,
    ‘author’,
    ‘categories’,
    ‘comments-link’,
    );
    } );

    add_filter( ‘generate_post_date_show_updated_only’, ‘__return_true’ );

    2) Added the following custom CSS:

    footer.entry-meta {
    display: none;
    }

    .comments-link:before,
    .cat-links:before {
    content: “\25CF”;
    margin: 0 10px;
    color: #888888;
    font-size: 10px;
    }

    .posted-on:before {
    content: “Last Updated: “;
    margin: 0;
    }

    .posted-on:after {
    content: “\25CF”;
    margin: 0 10px;
    color: #888888;
    font-size: 10px;
    }

    .post-image-below-header.post-image-aligned-center .inside-article .featured-image {
    margin-top: 20px !Important;
    }

    .entry-meta {
    margin-top: 20px;
    font-size: 14px;
    }

    .entry-meta a:hover {
    color: #24a2cc;
    }

    Thank you for your help and support with this, it is very much appreciated. Please note I have created a new admin account and provided you the login details and I am more than happy for you to login to the site and make any needed changes.

    #1742685
    Elvin
    Staff
    Customer Support

    Hi there,

    Remove this:
    add_filter( ‘generate_post_date_show_updated_only’, ‘__return_true’ );

    Use this instead:

    add_filter( 'generate_post_date_output', db_modified_time_stamp, 20, 2);
    
    function db_modified_time_stamp( $output, $time_string ) {
            $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on %2$s</time> ';
    
        if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
           	$time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">Last Updated: %4$s</time> ' . $time_string;
        }
    	
    	// get modified time and published time. Compare the two, If modified date exists then ouput both time strings with modified date
    	$updated_time = get_the_modified_time( 'U' );
        $published_time = get_the_time( 'U' ) + 86400;
        
        if ( $updated_time > $published_time ) {
            $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified" style="display:inline;">Last Updated: %4$s</time>';
        }
    
            $time_string = sprintf( $time_string,
                esc_attr( get_the_date( 'c' ) ),
                esc_html( get_the_date('F j, Y') ),
                esc_attr( get_the_modified_date( 'c' ) ),
                esc_html( get_the_modified_date('F j, Y') )
            );
    
            return 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
                )
            );
    }

    This filter checks if the post has modified date and displays it. Else, it displays the post date.

    After applying this, you don’t need to add the “Last Updated on:” through a :before pseudo element. I’ve included it on the render. Modify that part of the code if you wish to do so, just watch out for the syntax.

    #1742706
    Tim

    Okay thank you, I did what you suggested, but there’s still some things I still need help with please.

    1. I forgot to clarify that for the post that has NOT been updated, can that say “Published:” while the updated post says “Last Updated:” ?

    2. Can we remove the link that is now embedded within the date?

    3. This post ( https://waclasses.siterubix.com/example-post-not-updated ) should be showing the publish date of March 3, 2021 but instead it’s showing the updated date. So ideally, here’s what I’d love to see for this post – Published: March 3, 2021

    And for the other one ( https://waclasses.siterubix.com/example-of-an-updated-post/ ) – Last Updated: April 21, 2021

    Sorry to be a pain, and thank you so much for your time here.

    #1742732
    Elvin
    Staff
    Customer Support

    1. I forgot to clarify that for the post that has NOT been updated, can that say “Published:” while the updated post says “Last Updated:” ?

    That’s what the filter I’ve provided was tasked for. is it not working?

    2. Can we remove the link that is now embedded within the date?

    Sure, by modifying the filter to remove the <a> tags

    Here’s a modified one without the link.

    add_filter( 'generate_post_date_output', db_modified_time_stamp, 20, 2);
    
    function db_modified_time_stamp( $output, $time_string ) {
            $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on %2$s</time> ';
    
        if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
           	$time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">Last Updated: %4$s</time> ' . $time_string;
        }
    	
    	// get modified time and published time. Compare the two, If modified date exists then ouput both time strings with modified date
    	$updated_time = get_the_modified_time( 'U' );
        $published_time = get_the_time( 'U' ) + 86400;
        
        if ( $updated_time > $published_time ) {
            $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified" style="display:inline;">Last Updated: %4$s</time>';
        }
    
            $time_string = sprintf( $time_string,
                esc_attr( get_the_date( 'c' ) ),
                esc_html( get_the_date('F j, Y') ),
                esc_attr( get_the_modified_date( 'c' ) ),
                esc_html( get_the_modified_date('F j, Y') )
            );
    
            return sprintf( '<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok.
                sprintf( '%1$s',
                    $time_string
                )
            );
    }

    3. This post ( https://waclasses.siterubix.com/example-post-not-updated ) should be showing the publish date of March 3, 2021 but instead it’s showing the updated date. So ideally, here’s what I’d love to see for this post – Published: March 3, 2021

    Regardless of what you did, if you pressed “update” on the post, it will be log as post modified date. And that will be used by the filter I’ve provided.

    You can test if the filter actually works by publishing a new post without doing any updates. You should see the date being labeled “Published on”. This will change to “Last Updated on:” if you do any modification and pressed “Update”.

    #1742826
    Tim

    Brilliant, it is working exactly as you said. The problem was on my end, I thought that modifying the publish date on the first post was sufficient to make the post appear as “published on” but (as you pointed out) I needed to create an entirely new post. Now it is working exactly as I want it.

    Thank you so much for your help here and for replying so quickly!

    #1743803
    Elvin
    Staff
    Customer Support

    No problem. Glad to be of any help. 🙂

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