==================================================== =================================
解决方案代码:
$array = array();
$i = 1;
while( $i <= $instance['posts_num'] ) {
$array['tab'.$i.'_title'] = 'Category Heading';
$array['tab'.$i.'_thumb'] = 'Insert link here';
$array['tab'.$i.'_featured'] = 'Insert link here';
$array['tab'.$i.'_description'] = 'Insert category desciption';
$array['tab'.$i.'_link'] = 'Insert category link';
$i += 1;
}
$instance = wp_parse_args( (array)$instance, array(
'heading' => 'Featured Area',
'title' => '',
'posts_num' => 1,
(array) $array
) );
==================================================== =================================
如何使用 while 循环创建数组。
我正在尝试借助简单的 wordpress 插件中的 while 循环创建一个数组。我的代码如下:
$instance = wp_parse_args( (array)$instance, array(
'heading' => 'Featured Area',
'title' => '',
'posts_num' => 1,
$array = array();
$i = 1;
while( $i <= $instance['posts_num'] ) {
$array[] = 'tab'.$i.'_title' => 'Category Heading',
$array[] = 'tab'.$i.'_thumb' => 'Insert link here',
$array[] = 'tab'.$i.'_featured' => 'Insert link here',
$array[] = 'tab'.$i.'_description' => 'Insert category desciption',
$array[] = 'tab'.$i.'_link' => 'Insert category link',
$i += 1;
}
) );
我只是想确认我是否做得正确。
我想生成这样的数组:
'tab1_title' => 'Category Heading',
'tab1_thumb' => 'Insert link here',
'tab1_featured' => 'Insert link here',
'tab1_description' => 'Insert category desciption',
'tab1_link' => 'Insert category link',
'tab2_title' => 'Category Heading',
'tab2_thumb' => 'Insert link here',
'tab2_featured' => 'Insert link here',
'tab2_description' => 'Insert category desciption',
'tab2_link' => 'Insert category link',