如何创建分类模板?我必须更改此代码中的哪些内容?

时间:2015-03-28 作者:Riffaz Starr

我已经创建了custom post type 而且是taxonomies.代码为:

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

function register_cpt_portfolio() {

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

    $args = array( 
        \'labels\' => $labels,
        \'hierarchical\' => true,
        \'description\' => \'Test Portfolio Posts\',
        \'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'custom-fields\', \'page-attributes\' ),
        \'taxonomies\' => array( \'category\' ),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'menu_position\' => 2,

        \'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\' => \'page\'
    );

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

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

function register_taxonomy_portfolio_taxonomies() {

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

    $args = array( 
        \'labels\' => $labels,
        \'public\' => true,
        \'show_in_nav_menus\' => true,
        \'show_ui\' => true,
        \'show_tagcloud\' => true,
        \'show_admin_column\' => true,
        \'hierarchical\' => true,

        \'rewrite\' => true,
        \'query_var\' => true
    );

    register_taxonomy( \'portfolio_taxonomies\', array(\'portfolio\'), $args );
}
现在我在自定义帖子类型管理面板中看到了这一点

enter image description here

这很好,可以很好地创建自定义帖子类型和分类法。

我还创造了two taxonomies.

enter image description here

现在我想在中显示投资组合two templates 根据分类法。

如果自定义帖子类型的分类是taxtemp1 然后,它应该显示在前端的第一个分类模板中。

如果自定义帖子类型的分类是taxtemp2 然后,它应该显示在前端的第二个分类模板中。

为此,我创建了两个php文件。

taxonomy-portfolio\\u taxonomies-taxtemp1。php分类法-portfolio\\u分类法-taxtemp2。php我认为它应该可以工作,但当我访问页面时,它会显示/显示在默认页面模板中。我用的是“二十一二”主题。

enter image description here

enter image description here

为什么它没有显示在我创建的正确模板上(taxonomy-portfolio\\u taxonomies-taxtemp1.php和taxonomy-portfolio\\u taxonomies-taxtemp2.php)?

但存档页面对于此自定义帖子类型很好:http://localhost/wp-test/portfolio/

1 个回复
SO网友:Mayeenul Islam

我使用了:

$args = array(
    ...
    \'rewrite\' => array( \'slug\' => \'portfolio-taxonomies\' ),
    ...
);

register_taxonomy( \'portfolio_taxonomies\', array(\'portfolio\'), $args );
所以我的URL是可预测的。

你做错了一件事,你放的URL不正确。如果实现了重写规则,则它们将是:

http://localhost/wp-test/portfolio-taxonomies/taxtemp1/
http://localhost/wp-test/portfolio-taxonomies/taxtemp2/
要获取正确的URL,请进入:

/wp-admin/edit-tags.php?taxonomy=portfolio_taxonomies&post_type=portfolio
并将鼠标悬停在每个分类术语上,单击“查看”链接以触发其各自的URL。如果模板尚未触发,那么我们必须进一步考虑。

结束

相关推荐

Display taxonomy term slugs

我希望在循环中显示当前帖子的分类术语的slug列表。<?php $terms = get_the_terms( $post->ID, \'wpsc_product_category\' ); if ( !empty( $terms ) ){ $term = array_shift( $terms ); echo $term->slug; } ?> 我目前使用的函数可以工作,尽管它只显示第一个分类术语的slug,而不是全部。e、 g.一个岗位分配给多个岗位wpsc_prod