如何将类别名称回显到数组wordpress

时间:2013-11-09 18:51:11

标签: php wordpress

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

    $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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

不应该是:

'choices' => $categories

如果没有,你肯定错过了一个步骤:将每个类别与一个键或标签或任何需要的东西相关联。 (密钥不能是数组:是您的错误来自哪里。每个类别都需要一个密钥:而不是数组本身。)