[Resolved] Author archive title – Adding prefix

Home Forums Support [Resolved] Author archive title – Adding prefix

Home Forums Support Author archive title – Adding prefix

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1101367
    John

    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?

    #1101417
    Leo
    Staff
    Customer Support

    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 ๐Ÿ™‚

    #1101477
    John

    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”?

    #1101635
    Tom
    Lead Developer
    Lead Developer

    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;
    } );
    #1102315
    John

    Thanks Tom, that sounds like the solution I was looking for. I will give it a try this morning!

    #1102377
    Tom
    Lead Developer
    Lead Developer

    No problem ๐Ÿ™‚

    #1102510
    John

    Working perfectly, thanks.

    #1102716
    Tom
    Lead Developer
    Lead Developer

    Awesome, you’re welcome!

    #1291511
    Fabien

    Hi,

    Is this function still supposed to work ? I am trying it but no luck so far…

    #1291537
    David
    Staff
    Customer Support

    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.

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.