I have a CPT:
function CPT_init(){
register_post_type('custcpt', array(
'labels' => array(
'name' => __('Products'),
'singular_name' => __('Product'),
'all_items' => __('View Products'),
'add_new' => __('New Product'),
'add_new_item' => __('New Product'),
'edit_item' => __('Edit Product'),
'view_item' => __('View Product'),
'search_items' => __('Search Products'),
'no_found' => __('No Products Found'),
'not_found_in_trash' => __('No Products in Trash')
),
'public' => true,
'publicly_queryable'=> true,
'show_ui' => true,
'query_var' => true,
'show_in_nav_menus' => false,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => '/products/', 'with_front' => false),
'menu_position' => 21,
'supports' => array('title','editor', 'thumbnail'),
'has_archive' => true
));
}
add_action('init', 'CPT_init');
And Taxonomy with my CPT:
function custcpt_category_taxonomy() {
$labels = array(
'name' => __( 'Product Categories' ),
'singular_name' => __( 'Product Category' ),
'search_items' => __( 'Search Product Categories' ),
'all_items' => __( 'All Product Categories' ),
'parent_item' => __( 'Parent Product Category' ),
'parent_item_colon' => __( 'Parent Product Category:' ),
'edit_item' => __( 'Edit Product Category' ),
'update_item' => __( 'Update Product Category' ),
'add_new_item' => __( 'Add New Product Category' ),
'new_item_name' => __( 'New Product Category' ),
'menu_name' => __( 'Product Categories' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'show_in_nav_menus' => true,
'has_archive' => true,
'rewrite' => array('slug' => '/products/', 'with_front' => false),
);
register_taxonomy( 'custtax', 'custcpt', $args );
}
add_action( 'init', 'custcpt_category_taxonomy');
I have a products page /products/
and a taxonomy template page: taxonomy-custtax.php
, I’ve tried taxonomy.php
but it didn’t seem to matter. I’ve flushed my permalinks probably 15-20 times by now. It works fine with default but not with %postname%
. I’ve added this above my taxonomy:
function custom_taxonomy_flush_rewrite() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('init', 'custom_taxonomy_flush_rewrite');
So when I go to http://www.mywebsite.com/products/term
I get 404 for some reason or another. I swear taxonomy always gives me problems, every. single. time.
Remove the slashes on your rewrite slug. Should be like so:
Also, you can’t have the same slug for both a taxonomy and a custom post type. They need to be unique, otherwise WP won’t know which one applies to which.
Additionally, if you have the slug of your custom post type as
products
, you cannot have a page with a slug ofproducts
also, otherwise that will cause conflicts.Finally, for a custom taxonomy listing of these, you want the file to be titled
taxonomy-{taxonomy_name}.php
, so in your case, it should be titledtaxonomy-custtax.php
.For best luck, I would suggest taking this in steps:
First, setup the custom post type. Then, if you set the slug to
products
(after flushing rewrite rules), visit http://www.mysite.com/products (again, be sure nothing else in your install has a slug of products). You should see a listing of your products (assuming you have added some product entries).Then, add your
archive-custcpt.php
file to your theme, modify it in some unique way, and refresh. You should see that it is now loading your list of products within that new template.Then, add in your custom taxonomy. If your taxonomy slug is
prodcat
(for products category), you should be able to visit http://www.mysite.com/prodcat/%5Bterm%5D (where [term] = a product category you have set up).Lastly, add your
taxonomy-custtax.php
file, modify it in some unique way, and refresh. You should see the listing showing up in your new template file.Remove “/” from both slug and create a file
taxonomy-custtax.php
then it will be work fine.I am quoting your code below with corrected: