0
votes

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. enter image description here

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.

1

1 Answers

0
votes

Try this code. It will help you to achieve your url structure... Make sure you update permalinks after saving it to functions.php

function custom_produce_category_link( $link, $term, $taxonomy )
{
    if ( $taxonomy !== 'produce_category' )
        return $link;

    return str_replace( 'produce_category/', '', $link );
}
add_filter( 'term_link', 'custom_produce_category_link', 10, 3 );