AT9*_*T92 8 javascript php wordpress jquery customization
我正在研究WordPress主题,并添加了一个选择框,以允许用户选择其网站的字体。我以为我会使用Google字体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
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我使用以下代码向定制器添加了一个部分,进行设置和控制:
// 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',
) ) );
Run Code Online (Sandbox Code Playgroud)
以下代码应将选项附加到选择框:
$.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.")
}
});
Run Code Online (Sandbox Code Playgroud)
如果我将#font-select更改为body,它会很好地附加选项,但是我尝试将它们附加到选择框,但这是行不通的。知道为什么以及如何进行这项工作吗?
您可以在定制器管理面板的选择框中添加“选项”值,如以下代码所示:
您要求的完整代码
共有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' );
Run Code Online (Sandbox Code Playgroud)
现在添加脚本文件
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' );
Run Code Online (Sandbox Code Playgroud)
现在,最后一步,请在此文件上方添加的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.")
}
});
});
Run Code Online (Sandbox Code Playgroud)
我已经尝试过此代码,并且工作正常!
希望这对您有所帮助!
您的 AJAX 成功回调正在寻找带有id
as 的下拉列表font-select
。然而,Dropdown 的 id 是基于 Label (恰好是font
)的。
下拉列表的 ID 是通过方法中的以下代码行创建的render_content
。
<?php echo str_replace(' ','-',strtolower(esc_html( $this->label ))); ?>
Run Code Online (Sandbox Code Playgroud)
$this->label
这里会参考Fonts
. 由于您使用的是 strtolower ,因此它将是fonts
. 我建议您传递另一个变量id
并使用该变量填充它。