admin管理员组文章数量:1404754
I need to create a dropdown menu with "posts from a custom post type" as option.
This dropdown will be placed as custom meta box.
For example, I want all posts with the custom type "Video" as option in the select.
<select>
<option>post title n°1<option>
<option>post title n°2<option>
....
</select>
Thanks
I need to create a dropdown menu with "posts from a custom post type" as option.
This dropdown will be placed as custom meta box.
For example, I want all posts with the custom type "Video" as option in the select.
<select>
<option>post title n°1<option>
<option>post title n°2<option>
....
</select>
Thanks
Share Improve this question asked Dec 15, 2011 at 13:31 SteffiSteffi 7653 gold badges12 silver badges20 bronze badges 1- 1 Where do you want this metabox to appear? I mean which page? – Rutwick Gangurde Commented Dec 16, 2011 at 5:20
4 Answers
Reset to default 6Here is the code I'm using in a project I'm working on.
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
is used as the name and id of the select, $post_type
is the type you want to be made into the select and $selected
is the post id you want selected in the select box.
wp_dropdown_pages(array('post_type'=>'video'));
See: http://codex.wordpress/Function_Reference/wp_dropdown_pages
If you already know how to make the custom meta box, you can use
wp_dropdown_categories();
maybe like so :
wp_dropdown_categories('taxonomy=your_texonomy&hide_empty=0&orderby=name&name=types&show_option_none=Select type);
Since my last answer was considered more of a question. I'll answer with more of an answer. You could use the Magic Fields plugin 2 (note the 2 because that is a different but improved plugin). You can choose a 'related type' field from the admin boxes they offer. Of course you still can excavate how it's done in this plugin if you want to create this function yourself, but at least there is somebody who figured it out.
本文标签: Create a dropdown with Custom Post Types as option in admin
版权声明:本文标题:Create a dropdown with Custom Post Types as option in admin 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744842858a2628028.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论