Archived topic
Author archive title - Adding prefix
9 replies · Started by John on December 11, 2019
I would like to change the title of author archive pages to display as something like 'Posts by (author name)' instead of just '(author name)'.
This is controlled by the generate_archive_title function, but if I change something there then it will add the 'Posts by' to all archive page titles.
So, basically I want to change
<h1 class="page-title">
<?php the_archive_title(); ?>
</h1>
to
<h1 class="page-title">
Posts by <?php the_archive_title(); ?>
</h1>
on author archive pages only.
What is the best way to do that?
Hi there,
Would a CSS solution like this work for you?
.archive.author h1.page-title span.vcard:before {
content: "Posted by: ";
}
Adding CSS: https://docs.generatepress.com/article/adding-css/
Let me know if this helps :)
Yep, that could work I think. It definitely works from a visual point of view.
Just wondering how do search engines, etc. read and index that. Will they read the part added with CSS as part of the page title and index as "Articles by Bob" or just index it as "Bob"?
Hi there,
For search engines to read/index it, you'd have to add this PHP instead.
add_filter( 'get_the_archive_title', function( $title ) {
if ( is_author() ) {
the_post();
$title = sprintf( '%1$s Posted by: <span class="vcard">%2$s</span>',
get_avatar( get_the_author_meta( 'ID' ), 75 ),
get_the_author()
);
rewind_posts();
}
return $title;
} );
Thanks Tom, that sounds like the solution I was looking for. I will give it a try this morning!
No problem :)
Working perfectly, thanks.
Awesome, you're welcome!
Hi,
Is this function still supposed to work ? I am trying it but no luck so far...
Hi there,
yes it should still work.
If you want to raise a new topic where you can share a link to your Author page and we can take a look.