正在使用startbox管理面板在wordpress管理面板上工作,我希望在数组中列出所有带有slug和name的类别,但我似乎无法使其工作,这是我的代码
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
$args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
// This much above code is given by wordpress to get all categories, mine starts below
class sb_slider_settings extends sb_settings {
function sb_slider_settings() {
$this->slug = 'sb_slider_settings';
$this->options = array(
'on_off_news' => array(
'type' => 'select',
'default' => 'true',
'label' => __( 'Enable breaking news', 'startbox' ),
'options' => array(
'false' => __( 'No', 'startbox' ),
'true' => __( 'Yes', 'startbox' ),
)
),
'ticker_text' => array(
'type' => 'select',
'default' => 'true',
'class' => 'ticker_text',
'label' => __( 'Extract posts from', 'startbox' ),
'options' => array(
foreach($categories as $category) {
$category->slug => __( $category->name , 'startbox' ) ;
}
)
)
);
parent::__construct();
}
}
显示错误,
Parse error: syntax error, unexpected 'foreach' (T_FOREACH), expecting ')'
答案 0 :(得分:0)
您无法在数组中执行代码。数组旨在包含固定值。
http://es1.php.net/manual/en/language.types.array.php
要做你想做的事,你必须创建一个函数并在数组中调用该函数。
答案 1 :(得分:0)
但是,您可以使用array_map在数组的每个元素上调用相同的函数:
http://us1.php.net/manual/fr/function.array-map.php
在你的情况下,
'options' => array_map('callback', $categories),
和你的回调
function callback($category) {
return __( $category->name , 'startbox' );
);