使用wpdb检索数据以用于定制器控件

时间:2019-09-15 作者:joebegborg07

所以我有以下代码:

add_action(\'customize_register\', \'homepage_sections\');
//products
function homepage_sections($wp_customize){
    $wp_customize->add_panel(\'homepage_sections\', array(
        \'title\'             => \'Homepage Sections\',
        \'priority\'          => \'20\'
    ));
    $wp_customize->add_section(\'homepage_settings_section\', array(
        \'title\'             =>  \'Homepage settings\',
        \'panel\'             =>  \'homepage_sections\',
    ));
    $wp_customize->add_setting(\'homepage_settings_setting\', array(
        \'default\'           =>  1
    ));
    $wp_customize->add_control(\'homepage_settings_control\', array(
        \'section\'           =>  \'homepage_settings_section\',
        \'settings\'          =>  \'homepage_settings_setting\',
        \'label\'             =>  \'Number of sections\',
        \'description\'       =>  \'Number of sections in homepage\',
        \'type\'              =>  \'number\'
    ));


    global $wpdb;
    $sections=$wpdb->get_results(\'SELECT section_id, section_title FROM vt_homepage_sections;\');

    foreach($sections as $key){
        $section_id=$key->section_id;
        $cust_setting_id=$section_id.\'_setting\';
        $cust_control_id=$section_id.\'_control\';


        $wp_customize->add_setting($cust_setting_id,array(

        ));
        $wp_customize->add_control($cust_control_id,array(
            \'settings\'          =>  $cust_setting_id,
            \'section\'           =>  \'homepage_settings_section\',
            \'label\'             =>  \'test Control\'
        ));
    }
}
Issue当我不使用包含使用$wpdb获取的值的变量时,一切都正常。是否在customizer framework之后加载了$wpdb对象?

当我使用上面的代码时,上下文中的customizer对象应该出现在customizer面板中,但它们没有。如果您能提示我上面的代码有什么问题,我将不胜感激。

当做

J

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

因此,这里似乎有两个问题:首先,您似乎没有建立像下面这样的WP\\u Customize\\u控件的新实例,而且重要的是要记住,您必须至少为设置和控件提供最小数组项,否则它将不会显示,您在设置中有一个空数组。

您还想确保您的命名约定不是以数字开始的,所以我在下面将它们切换了。

以下是您可以尝试的代码更改:

add_action(\'customize_register\', \'homepage_sections\');
//products
function homepage_sections($wp_customize){
    $wp_customize->add_panel(
        \'homepage_sections\', 
        array(
            \'title\'             => \'Homepage Sections\',
            \'priority\'          => \'20\'
        )
    );
    $wp_customize->add_section(
        \'homepage_settings_section\', 
        array(
            \'title\'             =>  \'Homepage settings\',
            \'panel\'             =>  \'homepage_sections\',
        )
    );
    $wp_customize->add_setting(
        \'homepage_settings_setting\', 
        array(
            \'default\'           =>  1
        )
    );
    $wp_customize->add_control(
       new WP_Customize_Control(
          \'homepage_settings_control\', 
           array(
              \'section\'           =>  \'homepage_settings_section\',
              \'settings\'          =>  \'homepage_settings_setting\',
              \'label\'             =>  \'Number of sections\',
              \'description\'       =>  \'Number of sections in homepage\',
              \'type\'              =>  \'number\'
           )
        )
    );


    global $wpdb;
    $sections=$wpdb->get_results(\'SELECT section_id, section_title FROM vt_homepage_sections;\');

    foreach($sections as $key) :
        $section_id=$key->section_id;
        $cust_setting_id = \'setting_\' . $section_id;
        $cust_control_id = \'control_\' . $section_id;


        $wp_customize->add_setting(
            $cust_setting_id,
            array(
                \'default\'   => \'\',
                \'transport\' => \'refresh\',
            )
        );
        $wp_customize->add_control(
            new WP_Customize_Control(
                $cust_control_id,
                array(
                    \'settings\'          =>  $cust_setting_id,
                    \'section\'           =>  \'homepage_settings_section\',
                    \'label\'             =>  \'test Control\'
                )
            )
        );
    endforeach;
}

相关推荐

WooThemes-供应商/预订-允许供应商管理资源

我正在尝试从WooThemes为bookings插件带来新功能。在组合预订和供应商插件时,不允许供应商用户管理资源(资源是自定义帖子)。我将新功能添加到资源自定义帖子中,然后将这些功能添加到供应商角色(通过用户角色插件)现在,资源显示在供应商角色的管理菜单中,但当我尝试添加新资源时,会出现“您无权访问此页面”错误。我添加的新功能:https://i.stack.imgur.com/OCDlV.png添加到角色的功能:https://i.stack.imgur.com/5t696.png使用角色登录时显示的