1

只是想知道,是否可以为多种帖子类型设置一组类别?更具体地说,是否可以在自定义帖子中使用默认帖子类型的类别?

如果是这样,那么是否可以在循环中过滤这些类别,仅显示一个全局类别中包含的某些帖子类型?

提前致谢!

4

1 回答 1

3

是的。当您注册您的自定义帖子类型时,您可以将“分类”设置为包含您的自定义分类中的一个/两个,以及核心“类别”和/或“post_tag”分类的数组。

查询这些循环可以在对WP Query的标准调用中完成

这是一个例子。这是未经测试的,但应该让您了解如何设置所有内容:

函数.php

add_action( 'init', 'codex_custom_init' );
function codex_custom_init() {
    $args = array(
        'labels' => array(
            'name' => 'Books',
            'singular_name' => 'Book',
            'add_new' => 'Add New',
            'add_new_item' => 'Add New Book',
            'edit_item' => 'Edit Book',
            'new_item' => 'New Book',
            'all_items' => 'All Books',
            'view_item' => 'View Book',
            'search_items' => 'Search Books',
            'not_found' =>  'No books found',
            'not_found_in_trash' => 'No books found in Trash', 
            'parent_item_colon' => '',
            'menu_name' => 'Books'
        ),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true, 
        'show_in_menu' => true, 
        'query_var' => true,
        'rewrite' => array( 'slug' => 'book' ),
        'capability_type' => 'post',
        'has_archive' => true, 
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
        'taxonomies'=>array('category')
    ); 

    register_post_type( 'book', $args );
}

环形

$q = new WP_Query(array(
    'post_type'=>'book',
    'category_name'=>'fantasy'
));
if($q->have_posts()) : while($q->have_posts()) : $q->the_post();
    the_title();the_excerpt();
endwhile;endif;
于 2013-05-28T23:05:55.960 回答