按自定义分类筛选和查询我的产品(自定义帖子类型)


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

所以我的WordPress CMS已经走过了很长的路(我对WordPress了解了很多。我创建了一个名为Products的自定义帖子类型,它在这里:

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);  
}

然后是一个名为Category的自定义分类法,它在这里:

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'),
        )); 
}

我在wp管理菜单中创建了一个名为"产品"的页面,该页面有我制作的自定义模板。前往http://localhost/project/products打开它,它列出了所有发布类型的产品。我还为singleproducts.php制作了一个页面,显示单个产品帖子类型的详细信息。

我不确定如何创建一种方法,通过上面的自定义分类Category来过滤我的产品。假设我有一个产品是一个小工具。此小部件的Category为"Spring Loaded"。如果我想查看单个小部件,我可以直接链接到http://localhost/project/products/widget它会打开single-product.php页面,并在我格式化页面时显示Widget。但我只想链接到"Spring Loaded"类别,并在该自定义类别中列出我的所有产品。我相信每个类别都有它自己的鼻涕虫(例如弹簧加载的)。如果我去http://localhost/project/product-category/spring-loaded它会弹出一个页面,但我不知道它正在加载哪个文件,否则我可以修改输出。现在,它将每个产品加载为一个正常的帖子(包括日期、评论等)

所以我的问题是如何筛选这些类别?或者,为了更改链接自的页面的输出,我应该修改哪个文件http://localhost/project/product-category/spring-loaded?

我希望这是有道理的。非常感谢。

更新:啊。这现在有意义了。我改了这个:

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

到此:

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

尽管我不确定它到底做了什么。

我还创建了一个分类法-productcategory.php,并进行了一些快速格式化和测试,但它确实有效。我可以去http://localhost/project/productcategory/它将列出该类别的所有产品。

现在我需要像你一样更改重写规则吗?老实说,我不明白htaccess是如何工作的,所以我不知道是否需要更改它。谢谢

这和我几周前遇到的问题差不多。它已经在Wordpress.SE:https://wordpress.stackexchange.com/questions/14451/taxonomy-terms-and-template-files