Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I know this question has been asked a thousand times, but feel like I've looked at every single article on this issue, and none of them have worked for me.

All I want is for there to be a category page for my custom post type, products. So that I can type www.mysite.com/products and see all the products. Or if it's easier make it work like regular posts do www.mysite.com/category/products. Either works for me.

I have written the code in functions.php and also used plugins like CPT-onomy and CPT-UI to successfully make the custom post types and taxonomies. But this doesn't create the category page.

How is this accomplished? Please include instructions on permalinks if that is relevant to my situation too.

Here is the code I placed in functions.php. The second block that deals with taxonomies actually breaks the site. However, using the two plugins mentioned above, I've been able to successfully create taxonomies, but still can't get a category page.

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'product',
        array(
            'labels' => array(
                'name' => __( 'Products' ),
                'singular_name' => __( 'Product' )
            ),
        'public' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'show_ui' => true,
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
        'taxonomies' => array('category', 'post_tag')
        )
    );
}


function my_taxonomies_product() {
    $args = array();
    register_taxonomy( 'product_category', 'product' $args );
}

add_action( 'init', 'my_taxonomies_product', 0 );

updated code

function create_post_type() {
    register_post_type( 'product',
        array(
            'labels' => array(
                'name' => __( 'Products' ),
                'singular_name' => __( 'Product' )
            ),
        'public' => true,
        'has_archive' => true,
        'exclude_from_search' => true,
        'show_ui' => true,
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
        )
    );
}
add_action( 'init', 'create_post_type' );

function my_taxonomies_product() {
    $labels = array(
        'name'              => _x( 'Product Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Product Category', 'taxonomy singular name' ),
        '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' ),
    );
    $rewrite = array(
        'slug' => 'type',
        'with_front' => true,
        'hierarchical' => true,
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'rewrite' => $rewrite,
    );
    register_taxonomy( 'product_category', 'product', $args );
}
add_action( 'init', 'my_taxonomies_product', 0 );
share|improve this question
1  
add your post type and taxonomy registration code to your question. –  Milo Nov 15 '13 at 22:39

2 Answers 2

up vote 0 down vote accepted

First, your taxonomy code has a missing comma before $args which is causing a fatal error:

register_taxonomy( 'product_category', 'product', $args );

Enable debugging while working on code to see PHP errors, which would reveal the line number and error.

As for your post type, you have a page where you can view all products, which is the post type archive. This argument in your post type registration code enables that:

'has_archive' => true

But since you've only set it to true, it will use the post type name as the archive slug, so your archive is located at:

mysite.com/product/

To change that, simply set it to the slug you want to use:

'has_archive' => 'products'

Then your archive will be located where you want it:

mysite.com/products/

Remember to visit your Settings > Permalinks page after making changes that effect permalinks, this will flush the rewrite rules.

share|improve this answer
    
I knew it couldn't be that difficult. Thank you Milo. –  wetjosh Nov 16 '13 at 2:10
    
Milo, how would I then go about making subcategories for a custom post type? If for example I wanted an archive page for all the electronic products, book products etc. or any other product categories I might dream up? And what would be the URL of that archive page? –  wetjosh Nov 16 '13 at 2:56
    
those would be taxonomy term archives, and those URLs are set by the rewrite argument when you register a taxonomy. If you want to use the built in category taxonomy instead of a custom taxonomy, you need to add some code to get your product post type to show up on those pages. –  Milo Nov 16 '13 at 3:11
    
I see. I've added the rewrite argument now but when I click 'view' in the taxonomy admin page it takes me to the correct url www.mysite/products/books but no entries appear. It shows I've added some to the category (a number appears next to it) but no posts appear... –  wetjosh Nov 16 '13 at 4:41
    
that is strange. are you sure it's the same taxonomy you've added products to? in your post type registration, you're associating the built in category and tag taxonomies to your post type, but then you also associate the product_category custom taxonomy to products via the taxonomy registration code. you also don't set any name arguments, so it will show up as "Tags" in the admin UI, which may be confusing. –  Milo Nov 16 '13 at 5:20

If you use custom taxonomy post type, you can create taxonomy-your-taxonomy-post-type.php.

Let's say for your url www.mysite.com/products, your must create taxonomy-products.php

share|improve this answer
1  
Ok, I remember seeing that in some forms. But what do I put in that taxonomy-products.php document? –  wetjosh Nov 16 '13 at 1:20
    
It is ok, you can add category.php code to taxonomy-products.php file. I think that would works for you! :) –  por Nov 16 '13 at 9:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.