0

我正在尝试将类别名称放入以下数组

    $args = array(
        'orderby' => 'name',
        'order' => 'ASC'
    );
    $categories = get_categories($args);
    $this->settings['categoriesmain1'] = array(
        'section' => 'general',
        'title'   => __( 'Example Select' ),
        'desc'    => __( 'This is a description for the drop-down.' ),
        'type'    => 'select',
        'std'     => '',
        'choices' => array(
            $categories => $categories // here i am trying to echo them
        )
    );

choices默认情况下它们是这样的

'choices' => array(
      'Choice 1' => 'Other Choice 1',
      'Choice 2' => 'Other Choice 2',
      'Choice 3' => 'Other Choice 3'
)

但是当我尝试用它们来呼应它们时,$categories => $categories我得到了错误Illegal offset type

4

2 回答 2

2
$args = array(
        'orderby' => 'name',
        'order' => 'ASC',
        'hide_empty'               => 0,
    );
    $categories = get_categories($args);
    $categories_name = array();
    foreach($categories as $category){
        $categories_name[] = $category->name;
    }
    $this->settings['categoriesmain1'] = array(
        'section' => 'general',
        'title'   => __( 'Example Select' ),
        'desc'    => __( 'This is a description for the drop-down.' ),
        'type'    => 'select',
        'std'     => '',
        'choices' =>  $categories_name
        )
    );
$settings = get_option('mytheme_options');
$my_choices  = $settings['categoriesmain1'];
var_dump($my_choices);
于 2013-11-10T15:10:11.877 回答
1

那不应该是:

'choices' => $categories

?

如果没有,您肯定在某处遗漏了一步:将每个类别与键或标签或所需的任何内容相关联。(键不能是数组:是错误的来源。每个类别都需要一个键:不是数组本身。)

于 2013-11-09T18:57:50.967 回答