- This topic has 6 replies, 2 voices, and was last updated 3 years, 3 months ago by Elvin.
-
AuthorPosts
-
January 4, 2021 at 12:52 pm #1605478alex2k5
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!
January 4, 2021 at 4:55 pm #1605703ElvinStaffCustomer SupportHi,
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/8bc94ae43f67c40a077aefc8ec970fd0January 4, 2021 at 5:22 pm #1605720alex2k5Thanks.
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.
January 4, 2021 at 5:39 pm #1605734alex2k5OK, 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 ). ' '; }
January 4, 2021 at 5:43 pm #1605736ElvinStaffCustomer SupportTry 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.
January 4, 2021 at 5:56 pm #1605745alex2k5Hi 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.
January 4, 2021 at 6:10 pm #1605754ElvinStaffCustomer SupportNice 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. 😀
-
AuthorPosts
- You must be logged in to reply to this topic.