Archived topic
Display category meta next to date on Archive pages
9 replies · Started by Gianluca on June 2, 2022
Hi,
I'm using the following snippet
add_filter( 'generate_post_author_output', function() {
return sprintf( ' <span class="byline">%1$s</span>',
sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%3$s<a href="https://www.viaggiaregratis.eu/gianluca-orlandi" title="%1$s" rel="author"><span class="author-name" itemprop="name">%2$s</span></a></span>',
esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
esc_html( get_the_author() ),
get_avatar( get_the_author_meta( 'ID' ) )
)
);
} );
add_filter( 'generate_header_entry_meta_items', function() {
return array(
'author',
'comments-link',
'date',
);
} );
add_filter( 'generate_footer_entry_meta_items', function( $items ) {
return array(
'categories',
);
} );
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
printf( '<span class="posted-on">%s</span> ',
$time_string
);
}, 10, 2 );
in order to display Author name | date on Archive pages and blog posts like this https://imgur.com/a/Fh47Q4D
Now let's say that I'd like to display also the category meta next to the date Author name | date | category only on Archive pages and not single blog posts, is it possible?
I've tried to implement the code here https://generatepress.com/forums/topic/display-category-meta-next-to-date/ but is not working properly.
Thank you in advance!
Hi Gian,
Can you try modifying your PHP to something like this?:
add_filter( 'generate_post_author_output', function() {
return sprintf( ' <span class="byline">%1$s</span>',
sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%3$s<a href="https://www.viaggiaregratis.eu/gianluca-orlandi" title="%1$s" rel="author"><span class="author-name" itemprop="name">%2$s</span></a></span>',
esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
esc_html( get_the_author() ),
get_avatar( get_the_author_meta( 'ID' ) )
)
);
} );
add_filter( 'generate_header_entry_meta_items', function() {
return array(
'author',
'comments-link',
'date',
);
} );
add_filter( 'generate_footer_entry_meta_items', function( $items ) {
return array(
'categories',
);
} );
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
printf( ' | <span class="posted-on">%s</span> ',
$time_string
);
$categories_list = get_the_category_list( ', ' );
if ( $categories_list ) {
echo ' | <span class="categories">' . $categories_list . '</span>';
}
}, 10, 2 );
Kindly let us know how it goes!
Hi Fernando,
Thank you for your reply, it's almost perfect but as you can see from the link provided in the private information tab there's a double | | between the author name and the date, do you know how to fix it?
I'm also using this CSS:
.entry-meta {
font-size: 13px;
}
.entry-meta > *:last-child:first-child {
border-left: 0;
padding-left: 0;
margin-left: 0;
}
.byline img {
width: 25px;
height: 25px;
border-radius: 50%;
position: relative;
vertical-align: middle;
margin: 0 10px 0 0;
}
Thank you in advance.
Hi there,
You also have this CSS:
.comments-link, .posted-on {
border-left: 1px solid #ddd;
padding-left: 10px;
margin-left: 10px;
}
Just removeborder-left: 1px solid #ddd; from the above CSS.
Hi Ying,
it's working, thank you for your help!
One last thing, if I remove the author name through Appearance > Customize > Layout > Blog > Archive, is there a way to remove the vertical bar only on archive pages https://imgur.com/a/TMADJR8 ?
Thank you in advance.
Any chance you would consider creating a block element post meta template for this?
https://docs.generatepress.com/article/block-element-post-meta-template/
Then there should be no PHP and CSS required at all.
Hi Leo,
I know, that should be easier, but I didn't switch to Gutenberg yet and I was trying to find a different solution.
You can change this PHP code
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
printf( ' | <span class="posted-on">%s</span> ',
$time_string
);
$categories_list = get_the_category_list( ', ' );
if ( $categories_list ) {
echo ' | <span class="categories">' . $categories_list . '</span>';
}
}, 10, 2 );
to:
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
if(is_single()) {
printf( ' | <span class="posted-on">%s</span> ',
$time_string
);
}
if(! is_single()) {
printf( ' <span class="posted-on">%s</span> ',
$time_string
);
}
$categories_list = get_the_category_list( ', ' );
if ( $categories_list ) {
echo ' | <span class="categories">' . $categories_list . '</span>';
}
}, 10, 2 );
Thank you!
No problem :)