使用“for”循环的定制器设置

时间:2018-01-08 作者:Lukasz Formela

如果我这里缺少什么,请告诉我-我尝试使用创建多个自定义设置for 循环,但它们不会显示在自定义程序中。代码如下:

for ( $i = 1; $i < 7; $i++ ) :

    $wp_customize->add_setting( \'home_features_boxes_icon_\' . $i, array(
        \'default\'           => \'\',
        \'type\'              => \'theme_mod\',
        \'capability\'        => \'edit_theme_options\',
        \'sanitize_callback\' => \'esc_html\',
    ) );
    $wp_customize->add_control( \'home_features_boxes_icon_\' . $i . \'_c\', array(
        \'settings\'          => \'home_features_boxes_icon\' . $i,
        \'section\'           => \'home_features_boxes\',
        \'type\'              => \'text\',
        \'label\'             => esc_html__( $i . \' - Box Icon\', \'lucathemes\' ),
    ) );

endfor;
节名称正确,在for 回路工作正常。

谢谢,卢卡

1 个回复
最合适的回答,由SO网友:ghoul 整理而成

你失踪了_ 在里面add_control 参数:settings.这个settings 参数应为home_features_boxes_icon_\' . $i

此处为完整代码:

for ( $i = 1; $i < 7; $i++ ) :

    $wp_customize->add_setting( \'home_features_boxes_icon_\' . $i, array(
        \'default\'           => \'\',
        \'type\'              => \'theme_mod\',
        \'capability\'        => \'edit_theme_options\',
        \'sanitize_callback\' => \'esc_html\',
    ) );
    $wp_customize->add_control( \'home_features_boxes_icon_\' . $i . \'_c\', array(
        \'settings\'          => \'home_features_boxes_icon_\' . $i,
        \'section\'           => \'home_features_boxes\',
        \'type\'              => \'text\',
        \'label\'             => esc_html__( $i . \' - Box Icon\', \'lucathemes\' ),
    ) );

endfor;
旁注:esc_html 不是消毒功能。使用sanitize_text_field 用于文本字段。

结束