gpt4 book ai didi

javascript - 如何通过 API 调用附加到 WordPress 定制器中的选择框?

转载 作者:行者123 更新时间:2023-12-03 22:32:30 24 4
gpt4 key购买 nike

我正在开发 WordPress 主题,并且添加了一个选择框以允许用户为其网站选择字体。我想我应该使用 Google Fonts API 来获取字体列表,而不是手动添加所有 900 种字体,但是当我调用 API 时,我无法将返回的数据作为选项附加到选择框中。

这是我的选择框类的 PHP 代码:

    class Customize_Select_Control extends WP_Customize_Control {
public $type = 'select';

public function render_content() {
?>
<label>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<select <?php $this->link(); ?> id="<?php echo str_replace(' ','-',strtolower(esc_html( $this->label ))); ?>-select">
<option value="<?php echo $this->value(); ?>" selected><?php echo $this->value(); ?></option>
</select>
</label>
<?php
}
}

然后,我使用以下代码向定制器添加了一个部分、设置和控件:

    // Add Font Section
$wp_customize->add_section( 'fonts' , array(
'title' => __( 'Font', 'wordpress' ),
'priority' => 100,
'description' => __( 'Pick a font for your website.', 'wordpress' )
) );

// Add the setting & control for the font
$wp_customize->add_setting( 'font-select' , array(
'default' => 'Open Sans',
'transport' => 'postMessage',
) );
$wp_customize->add_control( new Customize_Select_Control( $wp_customize, 'font-select', array(
'label' => __( 'Font', 'wordpress' ),
'section' => 'fonts',
'settings' => 'font-select',
) ) );

以下代码应该将选项附加到选择框:

     $.ajax({
type: "GET",
url: "https://www.googleapis.com/webfonts/v1/webfonts?key=[REDACTED]&sort=popularity&fields=items",
dataType: "json",
success: function (result, status, xhr){
console.log(result.items);
for (var i = 0; i<result.items.length; i++){
var family = result.items[i].family;
console.log(family);
$('#font-select').append(`<option value="${family}">${family}</option>`);
}
},
error: function (xhr, status, error) {
alert("There was an error loading the Google fonts API: " + status + " " + error + " " + xhr.status + " " + xhr.statusText + "\n\nPlease save your changes and refresh the page to try again.")
}
});

如果我将#font-select更改为body,它会很好地附加选项,但无论我尝试将它们附加到选择框,它都不会'不工作。知道我为什么以及如何才能做到这一点吗?

最佳答案

您可以在定制器管理面板的选择框中添加选项值,如下代码:

您的要求的完整代码

  • 您只需在脚本中添加您的 google font api key
  • 在我使用“twentynineteen”主题 slug 名称的地方,您可以使用您的主题 slug 名称

共有 3 个部分:

1)  function mytheme_customize_register( $wp_customize ) {
//All our sections, settings, and controls will be added here


$wp_customize->add_section( 'fonts' , array(
'title' => __( 'Font', 'twentynineteen' ),
'priority' => 100,
'description' => __( 'Pick a font for your website.', 'twentynineteen' )
) );

// Add the setting & control for the font
$wp_customize->add_setting( 'font-select' , array(
'type' => 'select',
'default' => 'Roboto',
'transport' => 'postMessage',
) );

$wp_customize->add_control( 'font-select', array(
'type' => 'select',
'priority' => 10, // Within the section.
'section' => 'core', // Required, core or custom.
'description' => __( 'This is a date control with a red border.' ),
'choices' => array( // Optional.
'wordpress' => __( 'Roboto' ),
'hamsters' => __( 'Lato' ),
'jet-fuel' => __( 'Muli' ),

),
'label' => __( 'Font', 'twentynineteen' ),
'section' => 'fonts',
'settings' => 'font-select',
) );

}
add_action( 'customize_register', 'mytheme_customize_register' );

现在添加脚本文件

2) function add_font_scripts(){

wp_enqueue_script('custom_js_script', get_bloginfo('template_url').'/js/custom-scripts.js', array('jquery'));
}
add_action( 'admin_enqueue_scripts', 'add_font_scripts' );

现在最后一步,请在此文件 custom-scripts.js 中添加以下脚本,我们刚刚在上面排队

3)  var $= jQuery;
$(document).ready(function(){


$.ajax({
type: "GET",

url: "https://www.googleapis.com/webfonts/v1/webfonts?key=apikey&sort=popularity&fields=items",
dataType: "json",

success: function (result, status, xhr){
var outputstate = [];
console.log(result.items);
for (var i = 0; i<result.items.length; i++){
var family = result.items[i].family;
console.log(family);

outputstate.push('<option value="'+ family +'">'+ family +'</option>');
$('#_customize-input-font-select').html(outputstate.join(''));
}
},
error: function (xhr, status, error) {
alert("There was an error loading the Google fonts API: " + status + " " + error + " " + xhr.status + " " + xhr.statusText + "\n\nPlease save your changes and refresh the page to try again.")
}


});
});

我已经尝试过这段代码,它工作正常!

希望这篇文章对您有所帮助!

关于javascript - 如何通过 API 调用附加到 WordPress 定制器中的选择框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58114993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com