Wordpress自定义帖子类型和分类仍然显示帖子类型中的所有帖子

时间:2012-05-07 20:19:05

标签: wordpress

嘿大家我目前非常沮丧,无法弄清楚这一点已经遍布互联网。我有一个名为“组合”的自定义帖子类型,我有一个名为“类别”的分类,其中包含两类“摄影”和“目录设计”。我正在使用此代码

            query_posts(array(
            'post_type' => 'portfolio',
            'tax_query' => array(
                'taxonomy' => 'category',
                'field' => 'catalog-design',
                'terms' => 'catalog-design'
            )
        ));

出于某种原因,它不是在听分类法,只是在投资组合中展示一切。我也尝试了这个,它仍然无法正常工作

            query_posts(array(
            'post_type' => 'portfolio',
                            'category' => 'catalog-design'
        ));

如果有人知道如何解决这个问题,那么创建所有内容的代码也很棒

add_action('init', 'portfolio');

function portfolio(){

$labels = array(
    'name' => _x('Portfolio', 'edit and add portfolio items'),
    'singular_name' => _x('Portfolio Item', 'post type singular name'),
    'add_new' => _x('Add Portfolio Item', 'Portfolio Item'),
    'add_new_item' => __('Add New Portfolio Item'),
    'edit_item' => __('Edit Portfolio Item'),
    'new_item' => __('New Portfolio Item'),
    'view_item' => __('View Portfolio'),
    'search_items' => __('Search Portfolio'),
    'not_found' =>  __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => ''
);

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'menu_icon' => get_stylesheet_directory_uri() . '/library/images/portfolio.png',
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title', 'category', 'editor','thumbnail'),
    'taxonomies' => array('category', 'client')
  ); 

register_post_type( 'portfolio' , $args );

}

1 个答案:

答案 0 :(得分:0)

'category'查找ID。尝试:

query_posts(array(
        'post_type' => 'portfolio',
        'category_name' => 'catalog-design'
));

或者:

$cat = get_cat_ID('catalog-design');
query_posts(array(
        'post_type' => 'portfolio',
        'category' => $cat
));