[Resolved] Best Way to Add Time to Post Meta

Home Forums Support [Resolved] Best Way to Add Time to Post Meta

Home Forums Support Best Way to Add Time to Post Meta

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #1605478
    alex2k5

    Before I get into it:

    I am coming from Genesis, and they had a bunch of custom ways for doing things. Trying to get familiar with how it’s all done here, so excuse the newb questions.

    I am researching before posting, but so far there are multiple answers for similar things, yet all slightly different than what I’ve run in to.

    Anyway, on to the question.

    Looking to add in time to the post meta. Currently using

    add_filter( 'generate_header_entry_meta_items', function() {
        return array(
            'date',
            'author',
            'categories',
        );
    } );

    Which gives me:

    MONTH DAY, YEAR by AUTHOR CATEGORIES

    I can alter the date in Settings -> General to be:

    F j, Y g:i a

    instead of

    F j, Y

    But, I feel like that’s not right since there is a separate time format area there, and technically time can be called independently in PHP.

    So, what would be the best way to alter the entry meta to read like so:

    By AUTHOR on MONTH DAY, YEAR at TIME under CATEGORIES

    Thanks in advance!

    #1605703
    Elvin
    Staff
    Customer Support

    Hi,

    So, what would be the best way to alter the entry meta to read like so:

    By AUTHOR on MONTH DAY, YEAR at TIME under CATEGORIES

    You can change the order within the filter to follow this preferred format.

    add_filter( 'generate_header_entry_meta_items', function() {
        return array(
            'author',
            'date',
            'categories',
        );
    } );

    As for the “by”, it’s already added by default. The “under” however, can be added through pseudo elements.

    You can try this out:

    span.cat-links:before {
        content: "under ";
    }

    As for the post date, you may have to use generate_post_date_output filter to change how the date outputs things.

    Here are some reference for this:
    https://docs.generatepress.com/article/generate_post_date_output/
    https://gist.github.com/generatepress/8bc94ae43f67c40a077aefc8ec970fd0

    A wise man once said:
    "Have you cleared your cache?"

    #1605720
    alex2k5

    Thanks.

    Order – got that sorted after I posted.

    Under – thanks, wasn’t thinking css here, but it works.

    Time – I ran into both of those prior to your post, and played with them. Can’t get any remix of that to work for time.

    Generally the printf and sprintf method of calling things give me a headache and I’ve always stayed away from it unless absolutely necessary. As I read it it doesn’t “click” immediately, slows me down.

    My prior method of altering this line was done in one filter:

    add_filter( 'genesis_post_info', 'post_info_filter2' );
    function post_info_filter2($post_info) {
    
        $post_info = '[post_author_posts_link before="By "] <span class="sep">on</span> [post_date] <span class="sep">at</span> [post_time ] [post_categories before="under " sep=", "]';
        return $post_info;
    }

    But, they had shortcodes for generating each part with options per.

    I will keep trying.

    #1605734
    alex2k5

    OK, fiddled with the date output filter from your link.

    https://docs.generatepress.com/article/generate_post_date_output/

    Would this be an appropriate way of doing it within Generatepress? Any performance hits or otherwise negative dings by doing it this way?

    add_filter( 'generate_post_date_output','tu_add_to_post_date' );
    function tu_add_to_post_date( $output ) {
        return 'on ' . get_the_date( '', $post->ID ) . ' ' . get_the_time( '', $post->ID ). ' ';
    }
    #1605736
    Elvin
    Staff
    Customer Support

    Try using this PHP snippet.

    add_filter( 'generate_post_date_output', function() {
    	$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s at %5$s</time> ';
    
    	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
    		$time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">%4$s at %6$s</time> ' . $time_string;
    	}
    
    	$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' ) ),
    		esc_html( get_the_date( 'g:i A' ) ),
    		esc_html( get_the_modified_date( 'g:i A' ) )
    	);
    
    	return sprintf( '<span class="posted-on">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
    		)
    	);
    } );

    There shouldn’t be that much of a performance hit on this as its pretty simple.

    A wise man once said:
    "Have you cleared your cache?"

    #1605745
    alex2k5

    Hi Elvin,

    Thanks. 1 – didn’t need $post->ID in my code above and 2 – it worked, but was missing all the structured data that yours had.

    I’ve added in your code, but removed the link on the date:

    add_filter( 'generate_post_date_output', function() {
    	$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s at %5$s</time> ';
    
    	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
    		$time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">%4$s at %6$s</time> ' . $time_string;
    	}
    
    	$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' ) ),
    		esc_html( get_the_date( 'g:i A' ) ),
    		esc_html( get_the_modified_date( 'g:i A' ) )
    	);
    
    	return sprintf( '<span class="posted-on">on %1$s</span>', // WPCS: XSS ok, sanitization ok.
    		sprintf( '%1$s',
    			$time_string
    		)
    	);
    } );

    That seems to be working.

    Haven’t dug into code for years, so I’m rusty overall and the methods are new. I really appreciate your help.

    #1605754
    Elvin
    Staff
    Customer Support

    Nice one. Glad you glad it sorted. 🙂

    Haven’t dug into code for years, so I’m rusty overall and the methods are new. I really appreciate your help.

    No problem. Feel free to open up new topics of you have other questions. 😀

    A wise man once said:
    "Have you cleared your cache?"

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