自定义分类的模板

时间:2014-05-29 10:55:34

标签: php wordpress templates taxonomy

我花了很多时间找到解决方案,但没有运气。

我有一个名为配方的自定义帖子类型,它有食谱类型(分类法),例如A,B,C

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

我的自定义帖子类型为:recipe_cpt 和分类法是recipe_tx

我尝试了taxonomy.phptaxonomy-recipe_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 );

2 个答案:

答案 0 :(得分:0)

根据wordpress模板Heirarchy你可以创建这样的文件。

您的分类是= recipr_tx 您的分类值是= A

然后你创建两个文件,如

taxonomy-a.php
taxonomy-a-recipe_tx.php

这是Wordpress模板heirarchy。

我希望这完全适合你。

答案 1 :(得分:0)

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

如果您要重写自定义分类法的网址

$rewrite = array(
        'slug'                       => 'recipes',
        'with_front'                 => true,
        'hierarchical'               => true,
    );

然后你必须删除并重新创建重写规则。 为此,我在Codex

上找到了一个函数

只需在flush_rewrite_rules()

之后致电register_taxonomy()

最终代码将是

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 );
希望它会帮助别人!