- This topic has 63 replies, 3 voices, and was last updated 4 years, 8 months ago by Tom.
-
AuthorPosts
-
June 10, 2019 at 8:56 am #925022TomLead DeveloperLead Developer
Took some digging, but this should work now: https://generatepress.com/forums/topic/custom-author-archive-pages/#post-924072
June 10, 2019 at 2:54 pm #925272culpableHiya 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, etcDid this work in your testing?
June 10, 2019 at 4:00 pm #925309TomLead DeveloperLead DeveloperI 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(); } } );
June 15, 2019 at 5:56 am #930408culpableThank 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?
June 15, 2019 at 7:30 am #930578DavidStaffCustomer SupportHi there,
you would need to use a child theme to create your own author page. This will not be overwritten when the theme updates.
June 21, 2019 at 10:42 pm #937604culpableThanks 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?
June 22, 2019 at 4:41 am #937731DavidStaffCustomer SupportYes, that is correct.
June 23, 2019 at 12:18 am #938336culpableThank 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?
June 23, 2019 at 2:50 am #938396DavidStaffCustomer SupportYou 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' ); ?>
June 23, 2019 at 2:57 am #938402culpableThanks 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.
June 23, 2019 at 3:13 am #938407DavidStaffCustomer SupportI adjusted the code above to set the query to Pages – give that a try.
June 23, 2019 at 3:34 am #938413culpableHmmm 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
June 23, 2019 at 3:36 am #938415DavidStaffCustomer SupportHave you tried removing the other function you added?
June 23, 2019 at 3:43 am #938421culpableYes 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
June 23, 2019 at 4:59 am #938465DavidStaffCustomer SupportSo i took the GP Archive PHP and modified the loop in this GIST:
-
AuthorPosts
- You must be logged in to reply to this topic.