Wordpress Customizer API:访问变量

时间:2016-02-08 19:01:15

标签: php wordpress wordpress-theming

我有以下代码:

function color_scheme_customizer_register($wp_customize) {

  $wp_customize->add_section('front_page', array(
    'title'    => __('Front Page'),
    'priority' => 120,
  ));
  // MAIN IMAGE
  $wp_customize->add_setting('front_page_options[image_select]', array(
    'capability'        => 'edit_theme_options',
    'type'           => 'option',
  ));
  $wp_customize->add_control( new WP_Customize_Image_Control($wp_customize, 'image_select', array(
    'label'    => __('Main Image', 'themename'),
    'section'  => 'front_page',
    'settings' => 'front_page_options[image_select]',
  )));

  // FEATURE ONE
  $wp_customize->add_setting('front_page_options[feature_one_page]', array(
    'capability'     => 'edit_theme_options',
    'type'           => 'option',
  ));
  $wp_customize->add_control('feature_one_page', array(
    'label'      => __('Featured Page One'),
    'section'    => 'front_page',
    'settings'   => 'front_page_options[feature_one_page]',
    'type'           => 'dropdown-pages',
  ));
  $wp_customize->add_setting('front_page_options[feature_one_textarea]', array(
    'capability'     => 'edit_theme_options',
    'type'           => 'option',
  ));
  $wp_customize->add_control('feature_one_textarea', array(
    'label'      => __('Featured Page One Summary'),
    'section'    => 'front_page',
    'settings'   => 'front_page_options[feature_one_textarea]',
    'type'           => 'textarea',
  ));
  ...
}

我想访问front_page_array变量,但我能找到的只是创建一个新的.css电子表格来进行更改的文档。有没有办法可以专门访问变量,如下所示:

<?php get_header(); ?>

<?php get_customizer_variables('front_page_options'); ?>

<?php get_footer(); ?>

1 个答案:

答案 0 :(得分:1)

要检索已存储的值,您将使用get_theme_mod()或get_option(),具体取决于您设置的“类型”。如果没有给出类型,则默认为theme_mod。要检索字段,请使用以下内容:

get_option( 'front_page_options[image_select]', '' );

或者,如果您有多个选项,则可以在数组中检索所有选项,然后根据需要访问它们。

$front_page_options = get_option( 'front_page_options', '' );
$image_select = $front_page_options['image_select'];

这是一个非常简单的示例,但应该让您了解如何访问您要查找的值。