Override Taxonomy Template

时间:2013-05-20 作者:user990717

我试图列出与特定分类法相关的所有产品。由于某些原因,尝试访问时模板无法工作“http://www.myexample.com/product_categories/funiture/“,我试过了taxonomy.php, taxonomy-term.php 什么都没有。这是我的代码:http://pastie.org/7934453

add_action(\'init\', \'register_cpt_product\');

function register_cpt_product() {

    $labels = array(
        \'name\' => _x(\'Products\', \'product\'),
        \'singular_name\' => _x(\'Product\', \'product\'),
        \'add_new\' => _x(\'Add New Product\', \'product\'),
        \'add_new_item\' => _x(\'Add New Product\', \'product\'),
        \'edit_item\' => _x(\'Edit Product\', \'product\'),
        \'new_item\' => _x(\'New Product\', \'product\'),
        \'view_item\' => _x(\'View Product\', \'product\'),
        \'search_items\' => _x(\'Search Products\', \'product\'),
        \'not_found\' => _x(\'No products found\', \'product\'),
        \'not_found_in_trash\' => _x(\'No products found in Trash\', \'product\'),
        \'parent_item_colon\' => _x(\'Parent Product:\', \'product\'),
        \'menu_name\' => _x(\'Shop\', \'shop\'),
    );

    $args = array(
        \'labels\' => $labels,
        \'hierarchical\' => false,
        \'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'custom-fields\', \'revisions\', \'page-attributes\'),
        \'taxonomies\' => array(\'Product Categories\'),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'menu_position\' => 20,
        \'show_in_nav_menus\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => true,
        \'capability_type\' => \'post\'
    );

    register_post_type(\'product\', $args);
}

add_action(\'init\', \'register_taxonomy_product_categories\');

function register_taxonomy_product_categories() {

    $labels = array(
        \'name\' => _x(\'Product Categories\', \'product_categories\'),
        \'singular_name\' => _x(\'Product Category\', \'product_categories\'),
        \'search_items\' => _x(\'Search Product Categories\', \'product_categories\'),
        \'popular_items\' => _x(\'Popular Product Categories\', \'product_categories\'),
        \'all_items\' => _x(\'All Product Categories\', \'product_categories\'),
        \'parent_item\' => _x(\'Parent Product Category\', \'product_categories\'),
        \'parent_item_colon\' => _x(\'Parent Product Category:\', \'product_categories\'),
        \'edit_item\' => _x(\'Edit Product Category\', \'product_categories\'),
        \'update_item\' => _x(\'Update Product Category\', \'product_categories\'),
        \'add_new_item\' => _x(\'Add New Product Category\', \'product_categories\'),
        \'new_item_name\' => _x(\'New Product Category\', \'product_categories\'),
        \'separate_items_with_commas\' => _x(\'Separate product categories with commas\', \'product_categories\'),
        \'add_or_remove_items\' => _x(\'Add or remove product categories\', \'product_categories\'),
        \'choose_from_most_used\' => _x(\'Choose from the most used product categories\', \'product_categories\'),
        \'menu_name\' => _x(\'Product Categories\', \'product_categories\'),
    );

    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'show_in_nav_menus\' => true,
        \'show_ui\' => true,
        \'show_tagcloud\' => false,
        \'show_admin_column\' => false,
        \'hierarchical\' => true,
        \'rewrite\' => true,
        \'query_var\' => true
    );

    register_taxonomy(\'product_categories\', array(\'product\'), $args);
}

2 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

Take a look at the Codex:

  • taxonomy-{taxonomy}-{term}.php - 如果分类法是sometax,而分类法的术语是someterm,WordPress将查找
    taxonomy-sometax-someterm.php. 对于Post格式,分类法为“Post\\U格式”,术语为post-format-{format}.
    即。taxonomy-post_format-post-format-link.php
  • taxonomy-{taxonomy}.php - 如果分类法是某种税,WordPress会寻找taxonomy-sometax.php
  • taxonomy.php
您将分类注册为product_categories, 所以你应该使用taxonomy-product_categories.php 为该特定分类法中的所有术语创建模板。

使用taxonomy-product_categories-furniture.php 只针对URL中引用的一个术语。

taxonomy.php 将用于所有分类法。

SO网友:Ravinder Kumar

创建自定义帖子类型后是否保存永久链接

看看法典Custom Post Type Templates

结束

相关推荐

Custom taxonomy on permalink

我有一个很长时间的问题,期待你的帮助。我有一个自定义的“城市”分类法(它不像标记那样具有层次结构)。我只希望我的URL如下所示:/%city%/%category%/%postname%/ 这就是我想要完成的。http://example.com/city/moscow - 显示分类城市莫斯科的所有帖子http://example.com/category/shopping - 显示此类别中的所有帖子和所有城市http://example.com/category/shopping/superm