如何在自定义帖子类型中创建特色帖子?

时间:2011-02-21 作者:Amit Erandole

我是否会像处理普通帖子一样执行此操作?我使用$featured\\u cat全局变量并将其拉出。我也应该这样做吗?

还是应该使用关联的自定义分类法?或者我想得太多了?分类法真的有好处吗?

伙计们,正确的方法是什么

2 个回复
最合适的回答,由SO网友:Chris_O 整理而成

注册video post类型和;特色分类法““删除”;特色定制元选择框帖子类型:

function c3m_reg_vid_post() {
            
              $labels = array(
                \'name\' => _x(\'Videos\', \'post type general name\'),
                \'singular_name\' => _x(\'Video\', \'post type singular name\'),
                \'add_new\' => _x(\'Add New\', \'video\'),
                \'add_new_item\' => __(\'Add New Video\'),
                \'edit_item\' => __(\'Edit Video\'),
                \'new_item\' => __(\'New Video\'),
                \'view_item\' => __(\'View Video\'),
                \'search_items\' => __(\'Search Videos\'),
                \'not_found\' =>  __(\'No videos found\'),
                \'not_found_in_trash\' => __(\'No videos found in Trash\'), 
                \'parent_item_colon\' => \'\'
              );
              $args = array(
                \'labels\' => $labels,
                \'public\' => true,
                \'publicly_queryable\' => true,
                \'show_ui\' => true, 
                \'query_var\' => true,
                \'show_in_nav_menus\' => true,
                \'can_export\' => true,
                \'rewrite\' => array(\'slug\' => \'video\', \'with_front\' => false),
                \'capability_type\' => \'post\',
                \'register_meta_box_cb\' => \'c3m_video_meta\', //This is for our custom meta box
                \'hierarchical\' => false,
                \'menu_position\' => 10,
               \'taxonomies\' => array(\'featured\'),
                \'supports\' => array(\'title\', \'editor\' \'custom-fields\')
              ); 
              register_post_type(\'video\', $args );
}
分类法

再想一想,让我们为特色视频帖子使用一个自定义字段,并创建一个下拉选择自定义元框来选择帖子是否特色。

设置自定义元框:

//hook to add a meta box
add_action( \'add_meta_boxes\', \'c3m_video_meta\' );

function c3m_video_meta() {
    //create a custom meta box
    add_meta_box( \'c3m-meta\', \'Featured Video Selector\', \'c3m_mbe_function\', \'video\', \'normal\', \'high\' );
}

function c3m_mbe_function( $post ) {

    //retrieve the meta data values if they exist
    $c3m_mbe_featured = get_post_meta( $post->ID, \'_c3m_mbe_featured\', true );

    echo \'Select yes below to make video featured\';
    ?>
    <p>Featured: 
    <select name="c3m_mbe_featured">
        <option value="No" <?php selected( $c3m_mbe_featured, \'no\' ); ?>>No Way</option>
        <option value="Yes" <?php selected( $c3m_mbe_featured, \'yes\' ); ?>>Sure Feature This Video</option>
    </select>
    </p>
    <?php
}

//hook to save the meta box data
add_action( \'save_post\', \'c3m_mbe_save_meta\' );
function c3m_mbe_save_meta( $post_ID ) {
    global $post;
    if( $post->post_type == "video" ) {
        if ( isset( $_POST ) ) {
            update_post_meta( $post_ID, \'_c3m_mbe_featured\', strip_tags( $_POST[\'c3m_mbe_featured\'] ) );
        }
    }
}

}
这是我们刚刚创建的酷炫小功能视频帖子选择器:enter image description here

现在,让我们查询特色视频帖子:

$args = array(
    \'post_type\' => \'video\',
    \'meta_query\' => array(
        array(
            \'key\' => \'c3m_mbe_featured\',
            \'value\' => \'yes\',
            \'compare\' => \'NOT LIKE\'
        )
    )
 );
$query = new WP_Query( $args );

SO网友:wyrfel

和其他类型的帖子一样,真的。。。使用类别。。。或者您喜欢的任何其他方法(标记、自定义分类法、自定义元数据/字段等)。

结束