此函数的行为更像wp_dropdown_categories
function
public function wpse34320_get_dropdown_posts( $args = array( \'post_type\' => \'post\', \'show_option_none\' => \'Select a post\', \'name\' => null, \'selected\' => \'\', \'echo\' => true ) ){
$posts = get_posts(
array(
\'post_type\' => $args[\'post_type\'],
\'numberposts\' => -1
)
);
$dropdown = \'\';
if( $posts ){
if( !is_string($args[\'name\']) ){
$args[\'name\'] = $args[\'post_type\'].\'_select\';
}
$dropdown .= \'<select id="\'.$args[\'name\'].\'" name="\'.$args[\'name\'].\'">\';
$dropdown .= \'<option value="-1">\'.$args[\'show_option_none\'].\'</option>\';
$args[\'selected\'] = intval($args[\'selected\']);
foreach( $posts as $p ){
$selected = \'\';
if( $p->ID == $args[\'selected\'] ){
$selected = \' selected\';
}
$dropdown .= \'<option value="\' . $p->ID . \'"\'.$selected.\'>\' . esc_html( $p->post_title ) . \'</option>\';
}
$dropdown .= \'</select>\';
}
if($args[\'name\'] === false){
return $dropdown;
}
else{
echo $dropdown;
}
}
use $args=array(
\'show_option_none\' => \'Select a custom post type\',
\'post_type\' => $post_type,
\'name\' => $name,
\'selected\' => $selected_id,
\'echo\' => true
);
wpse34320_get_dropdown_posts($args);