Change the URL of Author Archives for a Custom Post Type wit... open

Change the URL of Author Archives for a Custom Post Type without a plugin. WordPress code example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: WordPress 4.0+, PHP 5.6 +

After creating a custom WordPress post type (For example: portfolio), you can see a list of all portfolios by simply going to the page – site.com/portfolio. The file archive.php is responsible for their output.

What if we want to display post of the portfolio type only of a certain user? The author’s archive file – author.php will help us with this:

  1. To do this, create a new file in the root directory of your theme and name it author-portfolio.php (Replace portfolio with your post type);
  2. Copy the contents of author.php into your author-portfolio.php file
Everything worked, but our posts will be opened at the url site.com/author/authorlogin/?post_type=portfolio

Agree, such a url does not look very good and is not practical.

How chenge URL-s to user friendly

Our goal is to bring the url site.com/author/authorlogin/?post_type=portfolio to site.com/portfolio/authorlogin which is much more understandable and practical.

Based on the code below, you can change the url to another as needed

Add the code below to your function.php file:

function custom_rewrite_rules() {
    add_rewrite_rule(
        '^portfolio/([^/]+)/?$',
        'index.php?author_name=$matches[1]&post_type=portfolio',
        'top'
    );
}
add_action('init', 'custom_rewrite_rules');


function custom_author_link($link, $author_id, $author_nicename) {
    $author = get_user_by('id', $author_id);
    if ($author && $author->user_nicename === $author_nicename) {
        return home_url(user_trailingslashit('portfolio/' . $author_nicename));
    }
    return $link;
}
add_filter('author_link', 'custom_author_link', 10, 3);


function custom_flush_rewrite_rules() {
    flush_rewrite_rules();
}
add_action('after_switch_theme', 'custom_flush_rewrite_rules');

сustom_rewrite_rules function:

Adds a rewrite rule for portfolio post archive URLs.
^portfolio/([^/]+)/?$ is a regular expression to match a URL. ([^/]+) is responsible for capturing the value (username) from the URL.

index.php?author_name=$matches[1]&post_type=portfolio – Tells WordPress how to handle URLs. In this case, this is a request for an archive of “portfolio” records of a specific user.

Custom_author_link function: used to change the default author link to include the path we created.
home_url(user_trailingslashit(‘portfolio/’ . $author_nicename)) – builds a URL using the user’s nickname and appends it to “portfolio/“.

Make sure you have saved the permalink settings after adding this code. Go to “Settings” -> “Permalinks” and click “Save changes“. This should update the rewrite rules and make the code work.
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation