注册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\'] ) );
}
}
}
}
这是我们刚刚创建的酷炫小功能视频帖子选择器:
现在,让我们查询特色视频帖子:
$args = array(
\'post_type\' => \'video\',
\'meta_query\' => array(
array(
\'key\' => \'c3m_mbe_featured\',
\'value\' => \'yes\',
\'compare\' => \'NOT LIKE\'
)
)
);
$query = new WP_Query( $args );