Archived topic
Custom Author Archive Pages
63 replies · Started by culpable on June 6, 2019
I tried copy-pasting that code into the author.php file, but the output is "Nothing Found", as shown here: https://imgur.com/qKIdvqD
Is this working for you on a test site? The other code I mentioned in my functions.php to assign pages to authors is currently disabled.
Thank you for your help Dave.
I tried altering the standard archive.php code's while loop with some basic iteration:
<?php
do_action( 'generate_before_main_content' );
if ( have_posts() ) :
do_action( 'generate_archive_title' );
$i = 0;
while ( have_posts() && $i < 5) : the_post();
get_template_part( 'content', get_post_format() );
$i++
endwhile;
generate_content_nav( 'nav-below' );
else :
get_template_part( 'no-results', 'archive' );
endif;
do_action( 'generate_after_main_content' );
?>
In combination with the code that was previously added to my functions.php (to make wordpress pages show up):
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 ) );
}
} );
This has had the effect of showing 5 wordpress pages... per page.
I don't understand why this code would generate wordpress pages on /author/*name*/page/2, /author/*name*/page/3, etc.
Surely the condition while ( have_posts() && $i < 5) would be met after 5 iterations, and that would be the end of it? Which part of the code is invoking this to continue onto the next page?
This test site, no functions etc just added child theme and included the author.php i provided above:
https://shakahara.assemblewp.com/author/opebo/
As you can see there is only 5 Pages being displayed - no pagination so /author/name/page/# returns no results.
So something else must be interfering.
The method i provided sets the query to the 5 latest pages. Wherease setting a loop would not change the query just restrict the number being displayed. Its the one reason i choose the less preferred method of query_posts
That's fantastic to hear David – thank you for going to such lengths to help me with this.
I deleted my functions.php file and copy-pasted the author.php file you created from here: https://gist.githubusercontent.com/diggeddy/0f746df9aea39f555b79ee5029b1f1d4/raw/336c87c32bbfe57315201240152e711fad30e33c/Author.php
I then cleared the wordpress cache, autoptimize cache, tried a new browser, and hard refreshed.
I then updated to version 2.3 of GP Premium (from 2.2.2). I also disabled the Elements -> Layout I had on Author Archives that was making the layout Content / Sidebar.
I tried everything I could to make this a "clean slate". But still, the author is showing "Nothing found". I'm really dumbfounded by this. What else could it possibly be?
So to confirm, you're definitely using wordpress pages (i.e. https://imgur.com/VLeN1Wp), not wordpress posts? And the author definitely has > 5 wordpress pages assigned to them?
Can you think of anything else this might be?
Yeah - its showing Pages, you can see the post-type is page in the markup and they all link to pages.
All the pages are from the same author, i even reduced the limit to 3 and that is also working.
Hmmm.... i need to have a think
In theory, using query_posts() like this is identical to use the pre_get_posts action. The latter is just a way to filter the query without needing to overwrite the template.
What if we did this as your only pre_get_posts function?:
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 ) );
$query->set( 'posts_per_page', 3 );
$query->set( 'paged', false );
}
} );
add_filter( 'generate_show_post_navigation', function( $show ) {
if ( is_author() ) {
$show = false;
}
return $show;
} );
It works on my test server.
Tom I tried using your code in my functions.php (with nothing else) combined with David's code in my author.php.
The result is that /author/*name*/ is showing 3 pages as desired. But /author/*name*/page/2, /author/*name*/page/3, and even /author/*name*/page/100 is showing the exact same 3 pages.
Were your results the same?
Note that this was the same even after I deleted the author.php file (and hence used the default code).
Yes, my results were the same. Would you prefer those pages went to a 404?
You don't need the author.php file.
Yes I'd prefer that it 404'd.
A lot of this is driven by a desire to have my author page indexable without superfluous pages of duplicate content.
I'm trying to make the author pages "useful" in combination with the author box that appears on the posts and author archives using the code David recommends here: https://generatepress.com/forums/topic/custom-author-archive-pages/page/3/#post-939362
I also just found this post: https://blogpioneer.com/author-box-generatepress/ describing the same thing. Side note – is the code recommended in step 4 still required to add Schema markup? Thank you very much for your help guys.
We could redirect them to a /not_found page or something similar. However, I have a feeling it would be better to just set up a static page using a plugin like WP Show Posts. Then just redirect the author archives (regardless of page) to that static page.
The schema isn't required but certainly doesn't hurt.
That sounds like a good solution.
If I make the author URL at /*name*/, how would I:
- Alter the standard author link that appears under the h1 of each article the author writes (i.e. https://imgur.com/VWaIpUI) so that instead of linking to
/author/*name*/it goes to/*name*/ - What alterations would I have to make to David's author box code (shown below) to link to these pages?
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' )
)
);
} );
Also after checking a page on the site in Google's structured data testing tool (https://search.google.com/structured-data/testing-tool), I noticed that it already has CreativeWork Author Schema. I'm guessing this has to do with the differences in David's code (above) vs. the code recommended on that blog post (https://blogpioneer.com/author-box-generatepress/#step4).
You could try this to change the link:
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' ),
'URL TO YOUR AUTHOR PAGE HERE',
/* 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' )
)
);
} );
Just replace the URL TO YOUR AUTHOR PAGE HERE with the correct URL.
Do you need help with the redirection as well?
The author name above has schema attached to it, but it shouldn't hurt to add it to the author box as well.
Would love help with the redirect as well :)
I’ll implement them both later today and let you know the results. Thank you so much for your help guys.
This should work for the redirect:
add_action( 'template_redirect', function() {
if ( is_author() ) {
wp_redirect( home_url( '/your-page' ), 301 );
die();
}
} );
Let me know :)
Thank you for the code Tom.
This solution seems to work, but only for a single author page. Is there any way to standardize this to work for all author URLs?