Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.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_Templates_Taxonomy - Fatal编程技术网

Php 自定义分类的模板

Php 自定义分类的模板,php,wordpress,templates,taxonomy,Php,Wordpress,Templates,Taxonomy,我花了很多时间来寻找解决办法,但运气不佳 我有一个自定义的Post类型,称为recipe,它有recipe类型(分类法),例如a、B、C 现在我想列出A型的所有食谱 在www.mydomain.com/recipes/A 我的自定义帖子类型是:recipe\u cpt 分类法是recipe\u tx 我试过taxonomy.php,taxonomy-recipe\u tx.php,但我得到了404 有人能帮我创建自定义分类的模板吗 这是我的自定义分类法 function reg_recipe_t

我花了很多时间来寻找解决办法,但运气不佳

我有一个自定义的Post类型,称为recipe,它有recipe类型(分类法),例如a、B、C

现在我想列出A型的所有食谱 在
www.mydomain.com/recipes/A

我的自定义帖子类型是:
recipe\u cpt
分类法是
recipe\u tx

我试过
taxonomy.php
taxonomy-recipe\u tx.php
,但我得到了404

有人能帮我创建自定义分类的模板吗

这是我的自定义分类法

function reg_recipe_taxtaxonomy() {

$labels = array(
    'name'                       => _x( 'Recipe Categories', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Recipe Category', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Recipe Types', 'text_domain' ),
    'all_items'                  => __( 'All Types', 'text_domain' ),
    'parent_item'                => __( 'Parent Type', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Type:', 'text_domain' ),
    'new_item_name'              => __( 'New Type Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Type', 'text_domain' ),
    'edit_item'                  => __( 'Edit Type', 'text_domain' ),
    'update_item'                => __( 'Update Type', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate Types with commas', 'text_domain' ),
    'search_items'               => __( 'Search Types', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove Types', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used Types', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
);
$rewrite = array(
        'slug'                       => 'recipes',
        'with_front'                 => true,
        'hierarchical'               => true,
    );
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'rewrite'                    => $rewrite,
);
register_taxonomy( 'recipe_tx', array( 'recipe_cpt' ), $args );

   }

      // Hook into the 'init' action
     add_action( 'init', 'reg_recipe_taxtaxonomy', 0 );

根据wordpress模板继承人权限,您可以创建如下文件

您的分类法是=recipr\u tx 您的分类法值为=A

然后创建两个类似于

taxonomy-a.php
taxonomy-a-recipe_tx.php
这是Wordpress模板继承权

我希望您能充分利用此功能。

我找到了解决方案。 为自定义分类法创建模板

如果您正在重写自定义分类的url

$rewrite = array(
        'slug'                       => 'recipes',
        'with_front'                 => true,
        'hierarchical'               => true,
    );
然后必须删除并重新创建重写规则。 为此,我在上找到了一个函数

只需在注册分类法()之后调用
flush\u rewrite\u rules()

最后的代码是

function reg_recipe_taxtaxonomy() {

$labels = array(
    'name'                       => _x( 'Recipe Categories', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Recipe Category', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Recipe Types', 'text_domain' ),
    'all_items'                  => __( 'All Types', 'text_domain' ),
    'parent_item'                => __( 'Parent Type', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Type:', 'text_domain' ),
    'new_item_name'              => __( 'New Type Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Type', 'text_domain' ),
    'edit_item'                  => __( 'Edit Type', 'text_domain' ),
    'update_item'                => __( 'Update Type', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate Types with commas', 'text_domain' ),
    'search_items'               => __( 'Search Types', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove Types', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used Types', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
);
$rewrite = array(
        'slug'                       => 'recipes',
        'with_front'                 => true,
        'hierarchical'               => true,
    );
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'rewrite'                    => $rewrite,
);
register_taxonomy( 'recipe_tx', array( 'recipe_cpt' ), $args );
flush_rewrite_rules();
}

      // Hook into the 'init' action
     add_action( 'init', 'reg_recipe_taxtaxonomy', 0 );
希望它能帮助别人