【问题标题】:Wordpress - Adding a post category box to custom post typeWordpress - 将帖子类别框添加到自定义帖子类型
【发布时间】:2011-11-19 13:38:23
【问题描述】:

我创建了一个名为“案例研究”的自定义帖子类型......我想要做的是在案例研究“添加案例研究页面”内显示带有普通帖子类别的框,这样我就可以选择与我正在制作的案例研究相关的帖子。

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

任何建议都会很棒。

干杯,

【问题讨论】:

    标签: wordpress


    【解决方案1】:

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

    '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

    【讨论】:

      【解决方案2】:

      试试这个或检查这个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' );
      

      【讨论】:

        【解决方案3】:

        哦,有趣。并不是说我以前做过这个,但我只是想到它就让人兴奋。我做的有些脏,请见谅。

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

        【讨论】:

        • 但是使用标签,是否能够获取该帖子的 URL? ...我完全不知道如何尝试这样做
        • 你必须记住,只要你能在那个循环中得到这些帖子。您始终可以获取有关该帖子的信息,例如标题、内容等。我认为值得一试,我做了一些类似的事情,但我想像其他项目一样在页面上显示其他自定义帖子类型。
        • 您是否有机会发布一些示例代码....我对此有点新意。 :)
        • 看看这个页面。 codex.wordpress.org/Function_Reference/query_posts有一些想法
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-24
        • 1970-01-01
        • 2013-08-04
        • 1970-01-01
        • 1970-01-01
        • 2012-01-01
        相关资源
        最近更新 更多