自定义帖子类别查询

时间:2014-12-20 14:02:09

标签: php wordpress

我注册了一个自定义帖子& function.php中的分类法。但是当我在模板中查询自定义帖子没有类别显示时的问题。我希望如(发布于:未分类)。我的自定义帖子注册,分类和&给定的自定义帖子查询代码。

    // register Custom Post Types
function create_post_types() {
    register_post_type(
        'portfolio', 
        array(
            'labels' => array(
                'name' => __('Portfolio'),
                'singular_name' => __('Portfolio'),
                'add_new' => 'Add New Portfolio',
                'edit_item' => 'Edit Portfolio',
                'capability_type' => 'post',
                'hierarchical' => false,
                'rewrite' => true,
                'query_var' => true
            ),
            'menu_position' => 6,
            'public' => true,
            'supports' => array(
                'title','editor','author','thumbnail','excerpt','comments','page-attributes'
            ),
            'rewrite' => array( 'slug' => 'portfolio', 'with_front' => false ),
            'taxonomy' => array('category', 'post_tag')
        )
    );    
}
add_action('init', 'create_post_types');


// register Taxonomies
function create_taxonomies() {
    register_taxonomy(
        'portfolio-category',
        'portfolio', 
        array (
        'hierarchical' => true, 
        'label' => __('Portfolio Categories'),
        'rewrite' => array(
            'slug'=> 'portfolio', 
            'with_front'    => false 
            )
        )
    );    
}
add_action('init', 'create_taxonomies');

1 个答案:

答案 0 :(得分:0)

我测试你的代码,问题很简单。你的参数数组中有一个拼写错误。要将自定义帖子类型与类别和标签相关联,您必须使用taxonomies而不是taxonomy作为数组键。所以你的代码应该像这样:

// register Custom Post Types
function create_post_types() {
    register_post_type(
        'portfolio', 
        array(
            'labels' => array(
                'name' => __('Portfolio'),
                'singular_name' => __('Portfolio'),
                'add_new' => 'Add New Portfolio',
                'edit_item' => 'Edit Portfolio',
                'capability_type' => 'post',
                'hierarchical' => false,
                'rewrite' => true,
                'query_var' => true
            ),
            'menu_position' => 6,
            'public' => true,
            'supports' => array(
                'title','editor','author','thumbnail','excerpt','comments','page-attributes'
            ),
            'rewrite' => array( 'slug' => 'portfolio', 'with_front' => false ),
            'taxonomies' => array('category', 'post_tag')
        )
    );    
}
add_action('init', 'create_post_types');