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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

Which 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