Archived topic
Problem adding author picture under blog post title
18 replies · Started by Benjamin on May 16, 2020
Hi,
I'm trying to add the author picture under the blog post title in single posts so I used the follow code:
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">%4$s<a href="%1$s" title="%2$s" rel="author"><span class="author-name" itemprop="name">%3$s</span></a></span>',
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() ),
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(
'date',
'categories',
);
} );
It worked but it added the pictures in blog archives as well. How can I only add the pictures in single posts?
Another problem I run into is that the comment count doesn't appear. So I added this code:
.single .comments-link {
display: inline-block;
}
Is this the right way to do it?
Thanks
I forgot to say I also added the following CSS to style the author picture (and show the last updated date instead of the published date:
.posted-on .updated {
display: inline;
}
.posted-on a time:nth-child(2) {
display: none;
}
.posted-on a time:first-child:before {
content: "Updated: ";
}
.posted-on a time:last-child:before {
content: "Published: ";
}
.byline img {
width: 35px;
height: 35px;
border-radius: 50%;
position: relative;
vertical-align: middle;
margin: 0 10px 0 0;
}
.comments-link,
.posted-on {
border-left: 1px solid #ddd;
padding-left: 10px;
margin-left: 10px;
}
Hi there,
Try this for your first function instead:
add_filter( 'generate_post_author_output', function( $output ) {
if ( ! is_singular() ) {
return $output;
}
return sprintf( ' <span class="byline">%1$s</span>',
sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%4$s<a href="%1$s" title="%2$s" rel="author"><span class="author-name" itemprop="name">%3$s</span></a></span>',
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() ),
get_avatar( get_the_author_meta( 'ID' ) )
)
);
} );
That comments CSS is the correct way to make it show up at the moment :)
That worked. Thanks :)
You're welcome :)
Hi,
I actually noticed a problem with the code you gave me.
It adds the date twice in archives:
The date shows up both under each post title, and under the excerpt.
Any idea how to fix it?
Thanks
Hi there,
do you still have this or similar function that uses the generate_footer_entry_meta_items filter applied?
add_filter( 'generate_footer_entry_meta_items', function( $items ) {
return array(
'date',
'categories',
'post-navigation',
);
} );
Yes, I did but deleting it solved my problem. Thanks! :)
Glad to hear that
Hi,
I used the codes you gave me but now the comment count shows up twice for every post on the archive page:
https://growwithless.com/blog/
Any idea what's causing this?
Thanks :)
try unchecking Display comment count in Customizer > Layout > Blog
I tried that but it deletes the comment count everywhere (including single posts.)
Just to confirm, so you want to show comment count in single posts but not in archives?
If so can you activate the option again we will need to use some CSS to hide it in archives.
Let me know :)
No, I would like to show it in both :). But it’s showing twice in archives. And if I deactivate comments count via the customizer, it disappears everywhere.
Sorry what code are you still using?
Can you activate the comments so I can see the issue?