从 WordPress 主题中的 Kirki 定制器字段获取值
Getting values from Kirki customizer fields in WordPress Theme
我正在使用 Kirki plugin 在 WordPress 定制器中添加字段和部分。到目前为止,我可以将一个字段添加到定制器中,但我对如何 return 将该数据返回到我的主题感到困惑。我有点累,所以我可能会遗漏一些东西。这是我目前所拥有的:
Kirki::add_config('theme_config_id', array(
'capability' => 'edit_theme_options',
'option_type' => 'theme_mod',
));
Kirki::add_section('footer_section', array(
'title' => __('Footer'),
'description' => __('Add custom footer here'),
'panel' => '', // Not typically needed.
'priority' => 160,
'capability' => 'edit_theme_options',
'theme_supports' => '', // Rarely needed.
));
Kirki::add_field('theme_config_id', [
'type' => 'editor',
'settings' => 'my_setting',
'label' => esc_html__('Footer Content', 'kirki'),
'description' => esc_html__('This content will show in the footer.', 'kirki'),
'section' => 'footer_section',
'default' => '',
]);
我正在阅读有关尝试使用此方法得出 from here 值的文章:
$value = Kirki::get_option( $config_id, $option_id );
但我不确定 $config_id
或 $option_id
在哪里(或什么)?
我觉得我遗漏了一些东西,我通读了文档,但我觉得我没有明白。
在互联网上稍微浏览了一下之后,我能够阅读更多文档以及其他一些示例,并且能够弄清楚我做错了什么。总的来说,我很接近,但我最终清理了它并直接在我的模板文件中使用了 WordPress get_theme_mod()
(在这种情况下它是 footer.php
文件)。
这是我最终得到的结果:
Kirki::add_config('theme_custom', array(
'capability' => 'edit_theme_options',
'option_type' => 'theme_mod'
));
Kirki::add_section('footer_section', array(
'title' => __('Footer'),
'description' => __('Add custom footer here'),
'panel' => '', // Not typically needed.
'priority' => 160,
'capability' => 'edit_theme_options',
'theme_supports' => '', // Rarely needed.
));
Kirki::add_field('theme_custom', array(
'type' => 'editor',
'settings' => 'footer_content',
'label' => esc_html__('Footer Content', 'kirki'),
'description' => esc_html__('This content will show in the footer.', 'kirki'),
'section' => 'footer_section',
'default' => '',
'priority' => 10
));
在我的 footer.php
文件中,我添加了这个:
<?php $value = get_theme_mod('footer_content', ''); ?>
<?php echo($value); ?>
当然,这是实现它的 super 基本方法。我将尝试弄清楚如何让它在发布之前刷新定制程序预览。但就目前而言,这似乎奏效了。
这是正确的代码
$value = Kirki::get_option( 'config_id', 'option_id' );
所以在你的情况下,
$value = Kirki::get_option( 'theme_custom', 'footer_content' );
我正在使用 Kirki plugin 在 WordPress 定制器中添加字段和部分。到目前为止,我可以将一个字段添加到定制器中,但我对如何 return 将该数据返回到我的主题感到困惑。我有点累,所以我可能会遗漏一些东西。这是我目前所拥有的:
Kirki::add_config('theme_config_id', array(
'capability' => 'edit_theme_options',
'option_type' => 'theme_mod',
));
Kirki::add_section('footer_section', array(
'title' => __('Footer'),
'description' => __('Add custom footer here'),
'panel' => '', // Not typically needed.
'priority' => 160,
'capability' => 'edit_theme_options',
'theme_supports' => '', // Rarely needed.
));
Kirki::add_field('theme_config_id', [
'type' => 'editor',
'settings' => 'my_setting',
'label' => esc_html__('Footer Content', 'kirki'),
'description' => esc_html__('This content will show in the footer.', 'kirki'),
'section' => 'footer_section',
'default' => '',
]);
我正在阅读有关尝试使用此方法得出 from here 值的文章:
$value = Kirki::get_option( $config_id, $option_id );
但我不确定 $config_id
或 $option_id
在哪里(或什么)?
我觉得我遗漏了一些东西,我通读了文档,但我觉得我没有明白。
在互联网上稍微浏览了一下之后,我能够阅读更多文档以及其他一些示例,并且能够弄清楚我做错了什么。总的来说,我很接近,但我最终清理了它并直接在我的模板文件中使用了 WordPress get_theme_mod()
(在这种情况下它是 footer.php
文件)。
这是我最终得到的结果:
Kirki::add_config('theme_custom', array(
'capability' => 'edit_theme_options',
'option_type' => 'theme_mod'
));
Kirki::add_section('footer_section', array(
'title' => __('Footer'),
'description' => __('Add custom footer here'),
'panel' => '', // Not typically needed.
'priority' => 160,
'capability' => 'edit_theme_options',
'theme_supports' => '', // Rarely needed.
));
Kirki::add_field('theme_custom', array(
'type' => 'editor',
'settings' => 'footer_content',
'label' => esc_html__('Footer Content', 'kirki'),
'description' => esc_html__('This content will show in the footer.', 'kirki'),
'section' => 'footer_section',
'default' => '',
'priority' => 10
));
在我的 footer.php
文件中,我添加了这个:
<?php $value = get_theme_mod('footer_content', ''); ?>
<?php echo($value); ?>
当然,这是实现它的 super 基本方法。我将尝试弄清楚如何让它在发布之前刷新定制程序预览。但就目前而言,这似乎奏效了。
这是正确的代码
$value = Kirki::get_option( 'config_id', 'option_id' );
所以在你的情况下,
$value = Kirki::get_option( 'theme_custom', 'footer_content' );