Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按自定义分类法筛选和查询我的产品(自定义帖子类型)_Php_Wordpress_Custom Post Type - Fatal编程技术网

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

Php 按自定义分类法筛选和查询我的产品(自定义帖子类型),php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,因此,我的WordPress CMS已经走过了相当长的一段路(我对WordPress有了相当多的了解。我创建了一个名为Products的自定义帖子类型,如下所示: add_action( 'init', 'create_product_post_type' ); function create_product_post_type() { $labels = array( 'name' => _x('Products', 'post type general na

因此,我的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);  
}
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'),
        )); 
}
然后是一个名为
Category
的自定义分类法,如下所示:

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);  
}
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 admin菜单中创建了一个名为Products的页面,它有一个我制作的自定义模板。打开它,它列出了所有的产品帖子类型。我还为single-Products.php创建了一个页面,其中显示了单个产品帖子类型的详细信息

我不确定如何创建一种方法,通过上面的自定义分类法
类别
来过滤我的产品。假设我有一个产品是一个小部件。这个小部件有一个
类别
“Spring-Loaded”。如果我想查看单个小部件,我可以链接到,它会显示
single-product.php
页面并在我格式化页面时显示小部件。但我只想链接到“Spring-Loaded”类别,并在该自定义类别中列出我的所有产品。我相信每个类别都有自己的slug(例如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),
        ));
尽管我不确定它到底是做什么的

我还创建了一个taxonomy-productcategory.php,并进行了一些快速格式化和测试,但它确实有效。我可以转到,它将列出该类别的所有产品


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

这与我几周前遇到的问题差不多。
它已经在Wordpress.SE上得到了回答:

当他们说制作模板页面“taxonomy type.php”时,是字面上的“taxonomy type.php”还是我应该用我的分类法slug(产品类别)替换分类法?我当前自定义的分类法slug重写看起来像这样
'rewrite'=>数组('slug'=>'product category'))
。我是否还需要将其更改为
“重写”=>数组('hierarchical'=>true)
?我不太确定这是怎么回事。如果它起作用,那么您就不必更改重写。