Wordpress - 将帖子类别框添加到自定义帖子类型

时间:2011-09-18 06:34:31

标签: wordpress

我创建了一个名为“案例研究”的自定义帖子类型...我希望能够做的是在案例研究的“添加案例研究页面”中显示正常帖子类别的框,这样我就可以选择与案例研究相关的帖子。

所有这些都在管理部分完成。

任何建议都会很棒。

干杯,

3 个答案:

答案 0 :(得分:2)

register_post_type函数的参数数组参数中,添加以下键和数组值,以在自定义帖子类型添加/编辑页面中添加常规类别:

'taxonomies' => array("category")

例如:

register_post_type( 'slider',
    array(
        'labels' => array(
        'name' => _x( 'Slider', 'post type general name' ),
        'singular_name' => _x( 'Slider', 'post type singular name' ),
        'add_new' => _x( 'Add New', 'Slider' ),
        'add_new_item' => __( 'Add New Slider' ),
        'edit_item' => __( 'Edit Slider' ),
        'new_item' => __( 'New Slider' ),
        'view_item' => __( 'View Slider' ),
        'search_items' => __( 'Search Slider' ),
        'not_found' =>  __( 'No Slider Items found' ),
        'not_found_in_trash' => __( 'No Slider Items found in Trash' ),
        'parent_item_colon' => ''
    ),
        'public' => true,
        'exclude_from_search' => true,
        'show_in_nav_menus' => false,
        'supports' => array('title','thumbnail'),
        'taxonomies' => array("category"),   /* This is what you are looking for and the value has to be an array. */
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
    )
);

有关详细信息,请参阅Wordpress register_post_type function documentation

答案 1 :(得分:1)

试试这个或查看tutorial

function Book() {
  $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'
  );

  $args = array(
    'labels'             => $labels,
    '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', 'post_tag')
  );

  register_post_type( 'book', $args );
}
add_action( 'init', 'book' );

答案 2 :(得分:0)

哦,有趣。不是说我以前做过这个,但我只是想到它让它变得令人兴奋。我做的事情有点脏,所以原谅我。

我会在自定义帖子中添加特殊标签。然后在我的自定义帖子中添加第二个循环,根据该标记查询帖子。