2

I'm currently working on my first in-depth custom template for Wordpress, utilizing the functionality of the customizer.

I am able to create options and also save them but somehow i am unable to push them to the page itself. The code i'm using:

//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' );

I am using this in my header.php(first lines) to try and retrieve the options (as they do save).

//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');

Any advice/feedback on this? I assume the faulty code is the last part. I tried following the codex, but i am lost at the data retrieval process.

4

1 回答 1

2

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

要解决此问题,请替换此

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

有了这个

extract(get_option('cbTheme_site_settings'));

这将为您提供直接来自选项表的以数组键为变量名的变量。

于 2017-03-22T08:47:52.997 回答