开发者

Filter and Query my Products (custom post type) by their Custom Taxonomy

So my WordPress CMS has come quite a long way (and I've learned quite a bit about WordPress. I've created a custom post type called Products which is here:

add_action( 'init', 'create_product_post_type' );

function create_product_post_type() 
{
    $labels = array(
        'name' => _x('Products', 'post type general name'),
        'singular_name' => _x('Product', 'post type singular name'),
        'add_new' => _x('Add New', 'product'),
        'add_new_item' => __('Add New Product'),
        'edit_item' => __('Edit Product'),
        'new_item' => __('New Product'),
        'view_item' => __('View Product'),
        'search_item' => __('Search Products'),
        'not_found' => __('No products found'),
        'not_found_in_trash' => __('No products found in Trash'),
        'parent_item_colon' => '',
        'menu_name' => 'Products'           
        );

    $args = array(
        'label' => __('Products'),
        'labels' => $labels,
        'public' => true,
        'can_export' => true,
        'show_ui' => true,
        '_builtin' => false,
        'capability_type' => 'post',
        'menu_icon' => get_bloginfo('template_url').'/functions/images/product.png',
        'hierarchical' => false,
        'rewrite' => array( "slug" => "product" ),
        'supports' => array('title'), //MAYBE add thumbnail later!
        'show_in_nav_menus' => true

        );

        register_post_type( 'product', $args);  
}

Then a custom taxonomy called Category which is here:

function create_productcategory_taxonomy() {
    $labels = array(
        'name' => _x( 'Categories', 'taxonomy general name' ),
        'singular_name' =>_x( 'Category', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Categories' ),
        'popular_items' => __( 'Popular Categories' ),
        'all_items' => __( 'All Categories' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Product Category' ),
        'update_item' => __( 'Update Product Category' ),
        'add_new_item' => __( 'Add New Product Category' ),
        'new_item_name' => __( 'New Product Category' ),
        'separate_items_with_commas' => __( 'Separate categories with commas' ),
        'add_or_remove_items' => __( 'Add or remove product categories' ),
        'choose_from_most_used' => __( 'Choose from the most used categories' )
        );

    register_taxonomy('productcategory', 'product', array (
        'label' => __('Product Category'),
        'labels' => $labels,
        'hierarchical' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'product-category'),
        )); 
}

I created a single page in the wp-admin menu called Products that has a custom template I made. Going to http://localhost/project/products brings it up and it lists all the products post types. I also made a page for single-products.php that displays a si开发者_StackOverflowngle product post type details.

What I'm not sure how to do is create a way to filter my products by the custom taxonomy Category that is above. Let's say I have a product that is a Widget. This Widget has a Category of 'Spring Loaded'. If I wanted to view the single widget, I can just link to http://localhost/project/products/widget and it would bring up the single-product.php page and display the Widget as I formatted the page to do so. But I would like to link to just the 'Spring Loaded' category and list all my products in that custom category. I believe every category has it's own slug (spring-loaded for example). If I go to http://localhost/project/product-category/spring-loaded it brings up a page but I dont' know which file it is loading, other wise I could modify the output. Right now it loads each product as a normal post (with the date, comments, etc...).

So my question is how can I filter these categories? Or which file do I modify in order to change the output of the page that is linked from http://localhost/project/product-category/spring-loaded ?

I hope this makes sense. Thank you!

UPDATE: Ahhhh. It's making sense now. I changed this:

 register_taxonomy('productcategory', 'product', array (
        'hierarchical' => true,
        'rewrite' => array( 'slug' => 'product-category'),
        ));

to this:

 register_taxonomy('productcategory', 'product', array (
        'hierarchical' => true,
        'rewrite' => array( 'hierarchical' => true),
        ));

Even though I'm not sure what it does exactly.

I also created a taxonomy-productcategory.php and did some quick formatting and testing but it does indeed work. I can go to http://localhost/project/productcategory/ and it will list all of the products for that category.

Now do I need to change my rewrite rules like you did? I honestly don't understand how htaccess works so I dont' know if I need to change it or not. Thanks!


This pretty much the same problem I've few weeks ago. It already answered on Wordpress.SE: https://wordpress.stackexchange.com/questions/14451/taxonomy-terms-and-template-files

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜