Wordpress检索自定义程序设置

时间:2017-03-21 15:45:51

标签: php wordpress wordpress-theming custom-theme

我目前正在使用自定义程序的功能,为Wordpress开发第一个深入的自定义模板。

我能够创建选项并保存它们,但不知怎的,我无法将它们推送到页面本身。我正在使用的代码:

//Do we want our template to be boxed or wide?
function cbTheme_customize_register($wp_customize){
    //  =============================
    //  = Add section               =
    //  =============================
    $wp_customize->add_section('cbTheme_general_settings', array(
        'title'    => __('General Settings', 'cbTheme'),
        'description' => 'Determines wether your site is boxed, or has 100% width',
        'priority' => 10,
    ));
    //  =============================
    //  = Add setting               =
    //  =============================
    $wp_customize->add_setting('cbTheme_site_settings[sitewidth]', array(
        'default'        => 'value1',
        'capability'     => 'edit_theme_options',
        'type'           => 'option',
    ));
    $wp_customize->add_control('cbTheme_site_width', array(
        'label'      => __('Site width', 'cbTheme'),
        'section'    => 'cbTheme_general_settings',
        'settings'   => 'cbTheme_site_settings[sitewidth]',
        'type'       => 'radio',
        'choices'    => array(
                'value1' => 'Wide',
                'value2' => 'Boxed',
        ),
    ));
}
add_action( 'customize_register', 'cbTheme_customize_register' );

我在header.php(第一行)中使用它来尝试检索选项(因为它们会保存)。

//GET ALL SETTINGS FROM CUSTOMIZER
function cbTheme_customize_css($wp_customize){
    $sitewidth = get_theme_mod('cbTheme_site_settings[sitewidth]');
}
add_action( 'wp_head', 'cbTheme_customize_css');

对此有何建议/反馈?我认为错误的代码是最后一部分。我尝试了跟踪编码,但我在数据检索过程中迷失了。

1 个答案:

答案 0 :(得分:0)

好的,我现在一直在寻找答案,现在找到了一个解决方法。如果其他人有更好的答案,请向我/其他用户提供。我使用的解决方法是应用于代码的第二部分,您可以在其中检索主题选项。

要解决此问题,请替换此

$sitewidth = get_theme_mod('cbTheme_site_settings[sitewidth]');

用这个

extract(get_option('cbTheme_site_settings'));

这将为您提供具有array-key作为变量名的变量,直接来自options-table。