我正在使用CMB框架创建元数据库(https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress)
我想添加一个选择框,它预先填充来自自定义帖子类型的标题。普通选择框如下所示:
array(
\'name\' => \'Test Select\',
\'desc\' => \'field description (optional)\',
\'id\' => $prefix . \'test_select\',
\'type\' => \'select\',
\'options\' => array(
array(\'name\' => \'Option One\', \'value\' => \'standard\'),
array(\'name\' => \'Option Two\', \'value\' => \'custom\'),
array(\'name\' => \'Option Three\', \'value\' => \'none\')
)
),
我想做如下事情:
array(
\'name\' => \'Test Select\',
\'desc\' => \'field description (optional)\',
\'id\' => $prefix . \'test_select\',
\'type\' => \'select\',
\'options\' => array(
query_posts( array( \'post_type\' => \'myposttype\' ) )
),
其中,选择选项是从自定义帖子类型的标题填充的。。但这不是我想的那样。有什么想法吗?
SO网友:Charles Clarkson
使用一个函数返回options
字段应为。
类似于以下内容(未测试的代码):
array(
\'name\' => \'Test Select\',
\'desc\' => \'field description (optional)\',
\'id\' => $prefix . \'test_select\',
\'type\' => \'select\',
\'options\' => get_myposttype_options(\'myposttype\'),
),
function get_myposttype_options($argument) {
$get_post_args = array(
\'post_type\' => $argument,
);
$options = array();
foreach ( get_posts( $get_post_args ) as $post ) {
$title = get_the_title( $post->ID );
$options[] = array(
\'name\' => $title,
\'value\' => $title,
);
}
return $options;
}