[Resolved] Custom Author Archive Pages

Home Forums Support [Resolved] Custom Author Archive Pages

Home Forums Support Custom Author Archive Pages

  • This topic has 63 replies, 3 voices, and was last updated 2 years ago by Tom.
Viewing 15 posts - 1 through 15 (of 64 total)
  • Author
    Posts
  • #921376
    culpable

    Hiya GP Support,

    I’m wondering what’s the best way for me to go about editing the Author Archive pages?

    Note that I’ve currently added this code to my pages as per the GP Press example site to add Author boxes:

    add_filter( 'generate_post_author_output', function() {
        printf( ' <span class="byline">%1$s</span>',
            sprintf( '<span class="author vcard" %5$s>%1$s <a class="url fn n" href="%2$s" title="%3$s" itemprop="url"><span class="author-name" itemprop="name">%4$s</span></a></span>',
    		__( 'by', 'generatepress' ),
    		esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    		/* translators: 1: Author name */
    		esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
    		esc_html( get_the_author() ),
    		generate_get_microdata( 'post-author' )
    	)
        );
    } );

    I’d like to make some adjustments to the Author pages so that they only show the most recent 3 posts of the author. I’d also like to add custom social buttons (Linkedin, Twitter, etc) to the page.

    What is the best way for me to go about editing these author archive pages?

    Thank you for your help 🙂

    #921435
    David
    Staff
    Customer Support

    Hi there,

    1. You can limit number of posts on author page with this function:

    function set_author_archive_limit( $query ) {
        if ( is_admin() || ! $query->is_main_query() )
            return;
    
        if ( is_author() ) {
            
            $query->set( 'posts_per_page', 3 );
            return;
        }
    }
    add_action( 'pre_get_posts', 'set_author_archive_limit', 1 );

    2. To make amends to the author page you would need either a plugin or child theme and create your own author page. This guide provides both options:

    https://www.wpbeginner.com/wp-themes/how-to-add-a-custom-author-profile-page-to-your-wordpress/

    An alternative route would be to register some new fields in your user Profile with this function:

    function thewebsitedev_user_social_links( $user_contact ) {
       /* Add user contact methods */
       $user_contact['facebook'] = __('Facebook Link', 'topaz');
       $user_contact['twitter'] = __('Twitter Link', 'topaz');
       $user_contact['dribbble'] = __('Dribbble Link', 'topaz');
       $user_contact['pinterest'] = __('Pinterest Link', 'topaz');
       $user_contact['linkedin'] = __('LinkedIn Link', 'topaz');
       $user_contact['googleplus'] = __('Google+ Link', 'topaz');
    
       return $user_contact;
    }
    add_filter('user_contactmethods', 'thewebsitedev_user_social_links');

    Source: https://thewebsitedev.com/social-links-wordpress-user-profile/

    Then hook the code they provide for displaying them using the hook element:

    https://docs.generatepress.com/article/hooks-element-overview/

    #922573
    culpable

    Thank you for your prompt reply and excellent suggestions David

    To clarify my previous comment: I meant that the author page should show the most recent 3 posts for the author with no other posts shown (i.e. right now it’s showing 3 *per page*. I don’t want all the posts to show (i.e. there shouldn’t be any pagination).

    Thank you for your assistance David

    #922766
    David
    Staff
    Customer Support

    Try switching where the code says posts_per_page for showposts

    #923026
    culpable

    Same result (I cleared the cache, and changed the number of posts from 3->5 to make sure it wasn’t a caching issue)

    #923211
    David
    Staff
    Customer Support

    Well this one got me puzzled – the original code works on my tests, not sure if that’s correct or not…..

    Would hiding the Nav on author pages with some CSS be ok, if so this:

    .archive.author #nav-below {
        display: none;
    }
    #923255
    culpable

    Hiding the Nav isn’t a good solution for me as part of what’s driving this is the desire to index author pages, without indexing 30 different pages via the pagination intrinsic to the author archives.

    If it works in your testing, then maybe the difference falls on the distinction between pages vs. posts? I don’t use wordpress “posts”, only wordpress “pages”. I failed to mention the difference until now as your previous change seemed to successfully alter the number of “pages” showing in the author archives, so I didn’t think it was relevant. For reference, this is the code I’m using to show pages on author archives:

    add_action( 'pre_get_posts', function( $query ) {
        if ( ! $query->is_main_query() || is_admin() ) {
            return;
        }
        if ( $query->is_author ) {
            $query->set( 'post_type', array( 'post', 'page' ) );
            $query->set( 'post__not_in', array(6539329,5533747,320,25753) );
        }
    } );

    (The “post__not_in” being used to exclude pages I do not wish to be included in the archives)

    Are you able to confirm that your suggested code works as intended for both pages and posts? 🙂

    #923472
    Tom
    Lead Developer
    Lead Developer

    What if we just disable pagination on author archives?:

    add_filter( 'generate_show_post_navigation', function( $show ) {
        if ( is_author() ) {
            $show = false;
        }
    
        return $show;
    } );
    #923704
    culpable

    Thank you for the code Tom.

    This removed the pagination, but the pages are still available at /author/*author name*/page/3

    This doesn’t quite solve the issue as I still can’t see the way for me to index /author/*author name*/ whilst deindexing all the other paginated pages.

    #924072
    Tom
    Lead Developer
    Lead Developer

    Let’s try replacing David’s code with this:

    add_action( 'pre_get_posts', function( $query ) {
        if ( is_admin() || ! $query->is_main_query() ) {
            return;
        }
    
        if ( is_author() ) {
            $query->set( 'posts_per_page', 3 );
            $query->set( 'no_found_rows', true );
        }
    } );

    Let me know 🙂

    #924300
    culpable

    Thank you for the code Tom.

    Unfortunately, this doesn’t seem to be working either. Whether isolated or combined with the previous code to disable pagination, the pages are still available at /author/*author name*/page/2 /author/*author name*/page/3 etc.

    Again I toggled the “showposts” 3->5 to make sure it was working. I also hard cleared the cache. Any other suggestions? 🙂

    #924317
    Tom
    Lead Developer
    Lead Developer
    #924322
    culpable

    Well – it got rid of the pagination!

    But.. it put all 1000 pages (by the Author) on the same author archive page.

    It doesn’t seem to be respecting the $query->set( 'showposts', 3 );

    #924325
    Tom
    Lead Developer
    Lead Developer
    #924506
    culpable

    Thank you Tom – still showing the same result as the previous code though :\

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