我想有一个元盒,让用户能够从下拉列表中选择一个网页从网站。我需要保存用户选择,以便以后使用。所以,我的问题是如何创建一个列出所有页面的元盒?
如何创建在下拉选择器中列出所有页面的metabox?
1 个回复
最合适的回答,由SO网友:Stephen Harris 整理而成
您可以使用wp_dropdown_pages
. 例如
add_action( \'add_meta_boxes\', \'myplugin_add_custom_box\' );
/* Adds a box to the main column on the Post edit screens */
function myplugin_add_custom_box() {
add_meta_box(
\'myplugin_sectionid\',
__( \'My Post Section Title\', \'myplugin_textdomain\' ),
\'myplugin_inner_custom_box\',
\'post\'
);
}
/* Prints the box content */
function myplugin_inner_custom_box( $post ) {
$dropdown_args = array(
\'post_type\' => \'page\'
\'name\' => \'myplugin[page]\',
\'sort_column\' => \'menu_order, post_title\',
\'echo\' => 1,
);
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin_noncename\');
//Dropdown of pages
wp_dropdown_pages( $dropdown_args );
}
另请参见the Codex 在处理metabox数据时。结束