下面是用于创建自定义post类型和自定义分类法的以下内容。
在“产品”部分中,我创建了“监视器”和“消耗品”类别。
然后,我创建了模板分类法-Monitors.php,这是否正确地命名为监视器类别?另外,我需要访问的url是什么,才能看到使用该模板的监视器类别?
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'products',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'capability_type' => 'post',
'supports' => array('title','editor','comments'),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'products' ),
)
);
}
function news_init() {
// create a new taxonomy
register_taxonomy(
'products',
'products',
array(
'label' => __( 'Product Categories' ),
'sort' => true,
'hierarchical' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array( 'slug' => 'products-category' )
)
);
}
add_action( 'init', 'news_init' );更新

发布于 2012-08-15 10:32:25
有关模板黑体如何选择模板的更详细的详细信息,请参见WordPress。
对于分类法术语slug (“监视”您的示例),分类法taxonomy (例如“产品”)中的WordPress将尝试使用以下模板(按此顺序)
taxonomy-{taxonomy}-{slug}.php
taxonomy-{taxonomy}.php
taxonomy.php
archive.php
index.php对于“监视器”分类法术语页,WordPress将使用
taxonomy-products-monitors.php如果它存在。如果它没有,那么对于这个分类法,它将回到
taxonomy-products.php诸若此类。
以下网址应指向“监视器”产品页面:
www.example.com?products=monitors您还指定了url重写,因此假设重写规则已经刷新,并且没有冲突,下面的操作也应该有效
www.example.com/products-category/monitorshttps://wordpress.stackexchange.com/questions/61896
复制相似问题