是否可以将多个图像保存到一个选项?

时间:2013-04-10 作者:Liam

以下代码允许您上载多个图像,但只有一个图像保存到选项中。是否可以将多个图像作为数组保存到选项中,或者是否必须创建单独的控件?

$wp_customize->add_section("slider",array(
    \'title\'=>"Slider",
    \'description\'=>"Choose the images for the slider",
    \'priority\'=>\'36\'
));

//images
$wp_customize->add_setting("slider[images]",array(
    \'default\'=>"",
    \'type\'=>"option"
));
$wp_customize->add_control(new WP_Customize_Image_Control($wp_customize,"slider[images]",array(
    \'label\'=>__("Images","adaptive-framework"),
    \'section\'=>"slider",
    \'settings\'=>"slider[images]"
)));

1 个回复
SO网友:tfrommen

您将设置定义为数组,但是,您会反复引用同一个条目slider[images].

$wp_customize->add_section("slider_images", array(
    \'title\' => "Slider",
    \'description\' => "Choose the images for the slider",
    \'priority\' => \'36\',
));

$wp_customize->add_setting("slider_images", array(
    \'default\' => "",
    \'type\' => "option",
));
$slider_images = get_setting(\'slider_images\');
$num_images = 0;
if (is_array($slider_images) && ! empty($slider_images))
    $num_images = count($slider_images);
$wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, "slider_images[".$num_images."]", array(
    \'label\' => __("Images", "adaptive-framework"),
    \'section\' => "slider",
    \'settings\' => "slider_images",
)));
Note: 我没有对此进行测试,而且我以前从未使用过WP的主题定制API。当然,您需要注意删除图像,并处理剩余的图像/数组条目等。这只是一个(很可能不起作用)示例。

如果要在单个设置中存储多个条目,请使用数组(例如。,slider_images). 然后,要定义一个特定条目,请引用特定的数组条目(例如。,slider_images[0]).

结束

相关推荐