Archived topic
Custom Author Archive Pages
63 replies · Started by culpable on June 6, 2019
Took some digging, but this should work now: https://generatepress.com/forums/topic/custom-author-archive-pages/#post-924072
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?
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();
}
} );
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?
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.
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?
Yes, that is correct.
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?
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' );
?>
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.
I adjusted the code above to set the query to Pages - give that a try.
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
Have you tried removing the other function you added?
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
So i took the GP Archive PHP and modified the loop in this GIST: