这是我在一个项目中使用的代码。
function generate_post_select($select_id, $post_type, $selected = 0) {
$post_type_object = get_post_type_object($post_type);
$label = $post_type_object->label;
$posts = get_posts(array(\'post_type\'=> $post_type, \'post_status\'=> \'publish\', \'suppress_filters\' => false, \'posts_per_page\'=>-1));
echo \'<select name="\'. $select_id .\'" id="\'.$select_id.\'">\';
echo \'<option value = "" >All \'.$label.\' </option>\';
foreach ($posts as $post) {
echo \'<option value="\', $post->ID, \'"\', $selected == $post->ID ? \' selected="selected"\' : \'\', \'>\', $post->post_title, \'</option>\';
}
echo \'</select>\';
}
$select_id
用作选择的名称和id,
$post_type
是要制作到select and中的类型
$selected
是要在选择框中选择的帖子id。