CMB select with data from CPT

时间:2013-08-27 作者:Rizzo

我正在使用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\' ) )
),
其中,选择选项是从自定义帖子类型的标题填充的。。但这不是我想的那样。有什么想法吗?

2 个回复
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;
}

SO网友:Prince Singh
query_posts( array( \'post_type\' => \'myposttype\' );

while ( have_posts() ) : the_post();

    $title_list[] =  array(\'name\' => the_title(), \'value\' => the_title());

endwhile;
wp_reset_query();

array(

    \'name\' => \'Test Select\',
    \'desc\' => \'field description (optional)\',
    \'id\' => $prefix . \'test_select\',
    \'type\' => \'select\',
    \'options\' => $title_list
)
结束

相关推荐

如何列出Metabox中的所有侧栏

我正在尝试向每个页面添加一个元框,该页面是所有已注册边栏的下拉列表,然后保存在自定义字段中。我在用这个metabox class我已经做到了这一点:// get registered Sidebars $sidebars = $GLOBALS[\'wp_registered_sidebars\']; foreach ( $sidebars as $sidebar ) { $sidebar_options[$sidebar[\'name\']] = $sidebar[\'na