admin管理员组文章数量:1287786
I want to add additonal sub pages to the author pages in WordPress. I have the following URL:
/author/USER_NAME/
But I also want to add URL's like this:
/author/USER_NAME/pinboard
/author/USER_NAME/teams
I've already done this to add a tag called author_view:
add_action('init', function( $vars ) {
add_rewrite_tag( '%author_view%', '([^&]+)');
}, 10, 1);
Which works great when going to this URL:
/?author=1&author_view=pinboard
And then I can just retrieve the author_view with get_query_var() and do whatever I want with it.
However, I'm a bit stuck in how to get the same page with pretty permalinks and a rewrite rule. This is my rewrite rule and template_redirect:
add_action('init', function( $vars ) {
add_rewrite_tag( '%author_view%', '([^&]+)');
global $wp_rewrite;
$author_base = $wp_rewrite->author_base;
add_rewrite_rule(
$author_base . '/([^/]*)/([^/]*)',
'index.php?author=$matches[1]&author_view=$matches[2]',
'top'
);
}, 10, 1);
add_action( 'template_redirect', function() {
$author_view = get_query_var( 'author_view', false );
if($author_view) :
include( get_template_directory() . "/author.php" );
exit();
endif;
} );
But then I get a bunch of errors since the queried object isn't the author object. Maybe I'm going about this the wrong way?
I want to add additonal sub pages to the author pages in WordPress. I have the following URL:
http://mylocalsite.site/author/USER_NAME/
But I also want to add URL's like this:
http://mylocalsite.site/author/USER_NAME/pinboard
http://mylocalsite.site/author/USER_NAME/teams
I've already done this to add a tag called author_view:
add_action('init', function( $vars ) {
add_rewrite_tag( '%author_view%', '([^&]+)');
}, 10, 1);
Which works great when going to this URL:
http://mylocalsite.site/?author=1&author_view=pinboard
And then I can just retrieve the author_view with get_query_var() and do whatever I want with it.
However, I'm a bit stuck in how to get the same page with pretty permalinks and a rewrite rule. This is my rewrite rule and template_redirect:
add_action('init', function( $vars ) {
add_rewrite_tag( '%author_view%', '([^&]+)');
global $wp_rewrite;
$author_base = $wp_rewrite->author_base;
add_rewrite_rule(
$author_base . '/([^/]*)/([^/]*)',
'index.php?author=$matches[1]&author_view=$matches[2]',
'top'
);
}, 10, 1);
add_action( 'template_redirect', function() {
$author_view = get_query_var( 'author_view', false );
if($author_view) :
include( get_template_directory() . "/author.php" );
exit();
endif;
} );
But then I get a bunch of errors since the queried object isn't the author object. Maybe I'm going about this the wrong way?
Share Improve this question asked Sep 9, 2021 at 11:43 Micke HasselqvistMicke Hasselqvist 153 bronze badges1 Answer
Reset to default 0Which works great when going to this URL:
http://mylocalsite.site/?author=1&author_view=pinboard
Yes, and that is because in that URL, the author
value is a user ID.
But in the "pretty" version of that URL (e.g. http://mylocalsite.site/author/admin/pinboard
), WordPress uses the user's "nice name" (from the user_nicename
column in the wp_users
table) which by default is the same as the username (from the user_login
column in the users table).
Therefore, in your rewrite rule's query (the 2nd parameter for add_rewrite_rule()
), you need to use author_name
and not author
:
add_rewrite_rule(
$author_base . '/([^/]*)/([^/]*)',
'index.php?author_name=$matches[1]&author_view=$matches[2]', // use author_name
// 'index.php?author=$matches[1]&author_view=$matches[2]', // and not "author"
'top'
);
That way, is_author()
would return true on the page (with the author_view
part in the URL) and thus, you'd not need to hook on template_redirect
because the author template like author.php
would be loaded correctly for you.
So in summary, just correct the query argument's name and your rule would work without giving you those errors. :) (But be sure to flush the rewrite rules — just visit the permalink settings page)
本文标签: rewrite rulesaddrewriteruleAdditional subpages for author pages
版权声明:本文标题:rewrite rules - add_rewrite_rule - Additional subpages for author pages 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741326391a2372506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论