[Resolved] Custom Author Archive Pages

Home Forums Support [Resolved] Custom Author Archive Pages

Home Forums Support Custom Author Archive Pages

Viewing 15 posts - 16 through 30 (of 64 total)
  • Author
    Posts
  • #925022
    Tom
    Lead Developer
    Lead Developer
    #925272
    culpable

    Hiya Tom,

    Appreciate your efforts – but there still seems to be pages appearing on: /author/*author-name*/page/2

    I have changed the posts_per_page from 3 -> 5 and hard refreshed to make sure this is not a caching issue (5 posts showed up per page, and there were still pages on /page/2, /page/3, etc

    Did this work in your testing?

    #925309
    Tom
    Lead Developer
    Lead Developer

    I can’t find a way to tell WP not to generate those pages.

    You could redirect those requests instead like this:

    add_action( 'template_redirect', function() {
    	if ( is_author() && is_paged() ) {
    		wp_redirect( home_url(), 301 );
    		die();
    	}
    } );
    #930408
    culpable

    Thank you for trying Tom. I’d rather not redirect >100 pages this way, so instead, I think I’ll pursue the method suggested by David (Method 2 here: https://www.wpbeginner.com/wp-themes/how-to-add-a-custom-author-profile-page-to-your-wordpress/).

    Is there a way for me to add an author page similar to how it is recommended in this article via code snippets instead of a child theme?

    I’m assuming if I made an author.php file (as recommended in the tutorial) that this would be overridden when updating to the latest version of generatepress? Or would this not be the new case since it is in addition to the existing files?

    #930578
    David
    Staff
    Customer Support

    Hi there,

    you would need to use a child theme to create your own author page. This will not be overwritten when the theme updates.

    #937604
    culpable

    Thanks for your help Dave.

    So if I make a child theme and add an author.php page (along with the standard functions.php file), will it be used automatically?

    #937731
    David
    Staff
    Customer Support
    #938336
    culpable

    Thank you David.

    I see in the archive.php file the posts are generated this way:

    <?php
    do_action( 'generate_before_main_content' );
    if ( have_posts() ) :
      do_action( 'generate_archive_title' );
      while ( have_posts() ) : the_post();
        get_template_part( 'content', get_post_format() );
      endwhile;
      generate_content_nav( 'nav-below' );
    else :
      get_template_part( 'no-results', 'archive' );
    endif;
    do_action( 'generate_after_main_content' );
    ?>

    Is there any way I can alter this to generate only the latest 5 posts, instead of all of them?

    #938396
    David
    Staff
    Customer Support

    You could try this:

    <?php
    do_action( 'generate_before_main_content' );
    query_posts(array(
        'post_type' => 'page',
        'showposts=5') );
    if ( have_posts() ) :
      do_action( 'generate_archive_title' );
      while ( have_posts() ) : the_post();
        get_template_part( 'content', get_post_format() );
      endwhile;
      generate_content_nav( 'nav-below' );
    else :
      get_template_part( 'no-results', 'archive' );
    endif;
    wp_reset_query();
    do_action( 'generate_after_main_content' );
    ?>
    #938402
    culpable

    Thanks for the code Dave.

    This edited version came up with a “Nothing Found”.

    I should note that there are only WordPress “pages” on the site – no wordpress “posts”.

    Previously the archive page was showing pages, but I believe that was because of this code in my functions.php:

    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( 258,599,607 ) );
        }
    } );

    I say this just in case it was the reason why the code you posted wasn’t working.

    #938407
    David
    Staff
    Customer Support

    I adjusted the code above to set the query to Pages – give that a try.

    #938413
    culpable

    Hmmm still doesn’t seem to be working.

    My PHP is poor, but I feel like we could target this part:

    while ( have_posts() ) : the_post();

    Could we somehow use wp_get_recent_posts()https://developer.wordpress.org/reference/functions/wp_get_recent_posts/ and iterate only through the first 5?

    Apologies for my ignorance David

    #938415
    David
    Staff
    Customer Support

    Have you tried removing the other function you added?

    #938421
    culpable

    Yes I did.

    It did some strange things, as shown here: https://imgur.com/a/rEuv7qs

    I noticed that:

    • No matter if I adjust the posts to 5, 3, or w/e else – it still shows the last 10 posts
    • The top part – with the author name and image – disappeared
    • For some reason the sidebar moved to below the posts
    #938465
    David
    Staff
    Customer Support

    So i took the GP Archive PHP and modified the loop in this GIST:

    https://gist.github.com/0f746df9aea39f555b79ee5029b1f1d4

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