- This topic has 3 replies, 2 voices, and was last updated 4 years, 4 months ago by Tom.
-
AuthorPosts
-
November 30, 2019 at 4:56 am #1086024Igor
Hi guys,
First, I would like to thank you for providing such amazing support on this forum, so far every problem I had was already solved here, each with a handy css or php code ready to just copy and paste. Amazing!
However, now I have a problem I wasn’t able to solve using existing advice. I would like to have following format of byline for my posts:
author – date published (updated: date modified)
On top of this I don’t want either author or those dates to be links.
So far, using code snippets and css already shared on this forum I was able to achieve this:
date published (updated: date modified) by author
Here are the PHP code snippets I’m using for this:
add_filter( 'generate_post_date_output', function( $output ) { $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>'; if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) { $time_string = $time_string . '<time class="updated" datetime="%3$s" itemprop="dateModified">%4$s</time>'; } $time_string = sprintf( $time_string, esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date() ), esc_attr( get_the_modified_date( 'c' ) ), esc_html( get_the_modified_date() ) ); echo sprintf( '<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok. sprintf( '%3$s', esc_url( get_permalink() ), esc_attr( get_the_time() ), $time_string ) ); } );
add_filter( 'generate_post_author_output','tu_no_author_link' ); function tu_no_author_link() { printf( ' <span class="byline">%1$s</span>', sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%1$s <span class="fn n author-name" itemprop="name">%4$s</span></span>', __( 'by','generatepress'), esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ), esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ), esc_html( get_the_author() ) ) ); }
And CSS:
.posted-on .updated { display: inline-block; margin-left:10px; } .posted-on .updated:before { content: "(updated: " } .posted-on .updated:after { content: ") " }
So it’s almost as I want it, the only problem which remains is to have the author at the beginning (and instead “by Author”, just “Author”).
November 30, 2019 at 5:54 pm #1087117TomLead DeveloperLead DeveloperHi there,
Try adding this as well:
add_filter( 'generate_header_entry_meta_items', function() { return array( 'author', 'date', ); } ); add_filter( 'generate_inside_post_meta_item_output', function( $output, $item ) { if ( 'author' === $item ) { return ''; } return $output; }, 15, 2 );
Let me know ๐
December 1, 2019 at 12:23 am #1087367IgorIt works! Thank you!
December 1, 2019 at 10:23 am #1088218TomLead DeveloperLead DeveloperYou’re welcome ๐
-
AuthorPosts
- You must be logged in to reply to this topic.