Site logo

[Support request] How to Customize Entry Meta

Home Forums Support [Support request] How to Customize Entry Meta

Home Forums Support How to Customize Entry Meta

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #1922719
    Tim

    Hello,

    A while back I contacted support via this forum to come up with a custom entry meta solution, and it is now exactly the way I want it (thank you). However, there is ONE thing I was hoping you might be able to help me with. I’d like to make the post date not display if the post is more than 24 months old. Including the CSS middle dot part.

    Here’s a screenshot to show you what I mean:

    Post meta

    So if the post is 24 months old or older, it would only show “By Tim * Product Reviews * Leave a comment”

    Here is the code I am currently using…

    PHP:

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

    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_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
    )
    );
    }

    CSS:

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

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

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

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

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

    —-

    Also, I currently am using a plugin called Code Snippets to insert the code for this custom entry meta and I was wondering if it’d be possible to implement this using a GeneratePress custom hook or something instead? Doesn’t matter if not, but it’d be nice to not have to use another plugin is all.

    Thank you for your help with this, it is very much appreciated.

    Tim

    #1922740
    Tim

    I should specify too… I’d like this to apply to posts that are 24 months old or older, and those that haven’t been edited within 24 months, please. So for example, if the post was published 23 months ago, or if it was last edited 23 months ago, the date will show. Thank you.

    #1923653
    Elvin
    Staff
    Customer Support

    Hi Tim,

    Try this one out.

    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') )
            );
    
        $post_date = new DateTime(get_the_date( 'Y-m-d' ));
        $modified_date = new DateTime(get_the_modified_date( 'Y-m-d' ));
        $current_date = new DateTime(date('Y-m-d'));
        $published_date_interval = $post_date->diff($current_date);
        $modified_date_interval = $modified_date->diff($current_date);
    
        if ($published_date_interval->y < 2 || $modified_date_interval->y < 2 ){
          return sprintf( <span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok.
                sprintf( '%1$s',
                    $time_string
                )
          );
        } if ($published_date_interval->y >= 2 || $modified_date_interval->y >= 2 ) {
          return;
        }
    }
    #1924942
    Tim

    Thanks Elvin, but that came up with a bunch of error messages when I inserted it, then broke my site when I submitted it. I can give you the login to the site if that helps?

    #1924999
    Elvin
    Staff
    Customer Support

    Ah right, I missed the single quotes.

    This one should be fine.

    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') )
            );
    
        $post_date = new DateTime(get_the_date( 'Y-m-d' ));
        $modified_date = new DateTime(get_the_modified_date( 'Y-m-d' ));
        $current_date = new DateTime(date('Y-m-d'));
        $published_date_interval = $post_date->diff($current_date);
        $modified_date_interval = $modified_date->diff($current_date);
    
        if ($published_date_interval->y < 2 || $modified_date_interval->y < 2 ){
          return sprintf( '<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok.
                sprintf( '%1$s',
                    $time_string
                )
          );
        } if ($published_date_interval->y >= 2 || $modified_date_interval->y >= 2 ) {
          return;
        }
    }

    Yes sure, you can provide the site details on the private information text field. 😀

    #1926557
    Tim

    Thanks Elvin, that didn’t break my site but now it’s not outputting as it did in the above screenshot. I’ve added the login details so I’m not sure if it’s asking too much but if you wouldn’t mind taking a look I’d really appreciate it.

    #1926702
    Elvin
    Staff
    Customer Support

    Not exactly sure what part are you pertaining to.

    Is it the dot not being vertically centered? If that’s the case consider adding this CSS:

    span.posted-on {
        display: inline-flex;
    }
    #1927224
    Tim

    I just mean the entry meta doesn’t display as it did with the old code.

    Before (original code):

    Before

    After (your code):

    After

    I want it to display exactly as it did before, I just don’t want the date part to show if the post is 24 months old or older. Or if it hasn’t been updated in more than 24 months.

    #1927275
    Elvin
    Staff
    Customer Support

    Ah I see what seems to be the issue here.

    The code I’ve provided was meant to replace only the part that used generate_post_date_output filter.

    You should still keep the other parts which are the PHP snippets using the filters generate_post_author_output, generate_header_entry_meta_items and this one:

    add_action( 'wp', function() {
        if ( is_single() ) {
            add_filter( 'generate_show_comments', '__return_true' );
        }
    } );
    #1927467
    Tim

    Brilliant that worked, thank you so much Elvin! Really appreciate your time with all of this.

    #1927498
    Elvin
    Staff
    Customer Support

    No problem. Glad to be of any help. 😀

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