【问题标题】:Creating fields through a loop and using vars - WordPress Customizer通过循环创建字段并使用 vars - WordPress Customizer
【发布时间】:2020-07-27 19:43:02
【问题描述】:

我正在 WordPress 的定制部分中创建一个新部分。此部分称为Footer Logos

网站的页脚中将有 7 张图片,我希望管理员能够完全控制这些图片的内容(这就是我通过定制器进行操作的原因)。

我没有通过 add_control 创建 7 个新控件,而是尝试通过将在第 7 次迭代时停止的循环来创建它们。

public function footer_logos($customizer) {
    // Add our customizer section
    $customizer - > add_section(
        'foot_logos', array(
            'title' => __('Footer Logos', 'my_theme'),
            'priority' => 1100,
        )
    );

    // Add our settings
    for ($i = 1; $i <= 7; $i++) {
        $customizer - > add_setting('logo-$i');
    }


    for ($i = 1; $i <= 7; $i++) {
        $customizer - > add_control(
            new WP_Customize_Image_Control(
                $customizer, 'logo-$i', array(
                    'label' => __('Logo $i', 'my_theme'),
                    'description' => __('', 'my_theme'),
                    'section' => 'foot_logos',
                )
            )
        );
    }



}

目前,仅显示一个标题为“Logo $i”的字段。

我想要实现的是带有标题“徽标 1”、“徽标 2”等的 7 个字段

【问题讨论】:

    标签: php wordpress customizer


    【解决方案1】:

    试试这个 -

    public function footer_logos($customizer) {
    // Add our customizer section
    $customizer - > add_section(
        'foot_logos', array(
            'title' => __('Footer Logos', 'my_theme'),
            'priority' => 1100,
        )
    );
    
    // Add our settings
    for ($i = 1; $i <= 7; $i++) {
        $customizer - > add_setting('logo-'.$i);
    }
    
    
    for ($i = 1; $i <= 7; $i++) {
        $customizer - > add_control(
            new WP_Customize_Image_Control(
                $customizer, 'logo-'.$i, array(
                    'label' => __('Logo '.$i, 'my_theme'),
                    'description' => __('', 'my_theme'),
                    'section' => 'foot_logos',
                )
            )
        );
    }
    
    
    
    }
    

    【讨论】:

      【解决方案2】:

      要打印变量 $i 你应该使用双引号。例如:"logo-$i"。另一种方式就像上面的答案'logo-' . $i

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-30
        • 2018-12-11
        • 1970-01-01
        • 2022-01-03
        • 2021-02-27
        • 2015-04-03
        • 1970-01-01
        相关资源
        最近更新 更多