最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Show custom post archive when custom post not specified

programmeradmin3浏览0评论

I have a custom post type called produce set up in WordPress and a custom taxonomy called produce_category.

The URL format for posts in this custom post type is in this format http://mywebsite/produce/%produce_category%/%postname%/. Of course %produce_category% and %postname% get substituted accordingly, a working example url would be http://mywebsite/produce/fruits-and-vegetables/avocado.

What I would like to do is show the produce_category custom taxonomy archive if a user visits http://mywebsite/produce/%produce_category%, without specifying post name at the end, e.g http://mywebsite/produce/fruits-and-vegetables to show all produce in fruits-and-vegetables produce_category.

Besides that when a user visits http://mywebsite/produce/ I would like to show all the produce archive. EDIT: I have this working now.

I know how to create the archive pages totally fine and have no problem with that. I am stuck at creating permalinks. When I visit http://mywebsite/produce/%produce_category% I get a 404 error.

I'm looking for advise on the best way to implement this. Currently I am using Custom Post Type Permalinks and CPTUI.

The CPTUI custom taxonomy settings interface does not allow me to have a blank in the custom rewrite slug. It defaults to the custom taxonomy slug, produce_category, when I don't fill in anything.

This gives the front-side produce_category taxonomy archive url as http://mywebsite/produce/produce_category/%produce_category%/ e.g. http://mywebsite/produce/produce_category/fish-and-seafood/ when what I want for the archive is http://mywebsite/produce/fish-and-seafood/.

Please help with suggestion on the best way I can achieve the custom taxonomy url.

Thank you all.

I have a custom post type called produce set up in WordPress and a custom taxonomy called produce_category.

The URL format for posts in this custom post type is in this format http://mywebsite/produce/%produce_category%/%postname%/. Of course %produce_category% and %postname% get substituted accordingly, a working example url would be http://mywebsite/produce/fruits-and-vegetables/avocado.

What I would like to do is show the produce_category custom taxonomy archive if a user visits http://mywebsite/produce/%produce_category%, without specifying post name at the end, e.g http://mywebsite/produce/fruits-and-vegetables to show all produce in fruits-and-vegetables produce_category.

Besides that when a user visits http://mywebsite/produce/ I would like to show all the produce archive. EDIT: I have this working now.

I know how to create the archive pages totally fine and have no problem with that. I am stuck at creating permalinks. When I visit http://mywebsite/produce/%produce_category% I get a 404 error.

I'm looking for advise on the best way to implement this. Currently I am using Custom Post Type Permalinks and CPTUI.

The CPTUI custom taxonomy settings interface does not allow me to have a blank in the custom rewrite slug. It defaults to the custom taxonomy slug, produce_category, when I don't fill in anything.

This gives the front-side produce_category taxonomy archive url as http://mywebsite/produce/produce_category/%produce_category%/ e.g. http://mywebsite/produce/produce_category/fish-and-seafood/ when what I want for the archive is http://mywebsite/produce/fish-and-seafood/.

Please help with suggestion on the best way I can achieve the custom taxonomy url.

Thank you all.

Share Improve this question asked Dec 31, 2019 at 8:56 Kha KaliKha Kali 411 gold badge1 silver badge2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Try to rewrite the taxonomy url in the .htaccess

RewriteRule ^produce_category(.*)$ /$1 [L]

(Not tested)

At the beginning I will indicate that the expected structure conflicts with the default for custom post type produce posts.

The solution to the problem requires changing the links created for the custom categories and appropriate parsing of new links to display the right posts.

Use the pre_term_link filter to change links. One of the parameters passed is the WP_Term object for which the link is generated. If its taxonomy is produce_category then the new {cpt}/%{taxonomy}% structure should be returned, that is produce/%produce_category%.

add_filter( 'pre_term_link', 'se355448_pre_term_link', 10, 2 );

function se355448_pre_term_link( $termlink, $term )
{
    if ( ! $term instanceof \WP_Term || $term->taxonomy != 'produce_category' )
        return $termlink;

    return '/produce/%produce_category%';
}

Then, for links to be recognized correctly, use the add_rewrite_rule() function to add the necessary rewrite rules. As already mentioned, your target structure collides with links to custom post type produce posts, that's why you need rewrite rules for every term in produce_category taxonomy, and they should be before CPT produce rules.

add_action( 'init', 'se355448_rewrite_rules' );

function se355448_rewrite_rules()
{
    $cpt = 'produce';
    $tax = 'produce_category';
    //$terms = ['fruits-and-vegetables', 'fish-and-seafood'];
    $terms = get_terms([
        'taxonomy'  => $tax,
        'hide_empty' => false,
        'fields'    => 'id=>slug',
    ]);
    if ( is_array($terms) && count($terms) )
    {
        foreach( $terms as $slug )
        {
            add_rewrite_rule(
                "{$cpt}/{$slug}(?:/page/([0-9]+))?/?$",
                "index.php?{$tax}={$slug}&taxonomy={$tax}&post_type={$cpt}&paged=\$matches[1]",
                'top'
            );
        }
    }
}

Each term needs a rewrite rule, so after adding a new one, deleting it, or changing slug of existing one, rewrite rules need to be refreshed. This can be done by clicking "Save" in "Settings -> Permalinks".

The code below will automatically, using the created_{$taxonomy} and delete_{$taxonomy} action hooks, update rewrite rules after adding or removing terms from produce_category taxonomy.

add_action( 'created_produce_category', 'se355448_refresh_rules', 20);
add_action( 'delete_produce_category',  'se355448_refresh_rules' );

function se355448_refresh_rules()
{
    se355448_rewrite_rules();
    flush_rewrite_rules();
}
发布评论

评论列表(0)

  1. 暂无评论