php - Wordpress 主题开发和 Google Font API
问题描述
我正在开发一个主题,我得到了这段代码,它可以下拉谷歌字体。下拉列表工作正常,但我的问题是我似乎可以让它在我的 css 中工作。
function ounox_fonts_setup($wp_customize) {
$url = 'https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyDwe8tl4YMbg8asbjzbXDDFuxzR1Wm9EQ0';
$response = wp_remote_get( 'http://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyDwe8tl4YMbg8asbjzbXDDFuxzR1Wm9EQ0', array( 'sslverify' => true ) );
$response = wp_remote_retrieve_body( wp_remote_get($url, array('sslverify' => false )));
if( is_wp_error( $response ) ) {
echo 'Something went wrong!';
} else {
$json_a = json_decode($response, true);
$font_items = $json_a['items'];
$choices = array();
foreach ($font_items as $font_value => $font_item) {
$choices[$font_item['family']] = $font_item['family'].' (Google)';
}
$font_args = array(
'label' => 'Fonts Section',
'section' => 'ounox_fonts_section',
'settings' => 'ounox_fonts_display',
'type' => 'select',
'choices' => $choices
);
}
$wp_customize->add_section( 'ounox_fonts_section', array(
'title' => 'Ounox Fonts'
));
$wp_customize->add_setting( 'ounox_fonts_display', array(
'transport' => 'refresh',
));
$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'ounox_fonts_display_control', $font_args));
}
add_action( 'customize_register','ounox_fonts_setup' );
还有这将 css 应用于我的主题。
function ounox_customize_css() { ?>
<style type="text/css">
html, body {
font-family: <?php echo get_theme_mod('ounox_fonts_display'); ?>;
}
</style>
<?php }
add_action('wp_head', 'ounox_customize_css');
如何将用户选择的字体应用于 CSS?对不起,后端的东西真的很糟糕。
解决方案
在functions.php中添加以下函数
function calling_google_font_script(){
// Getting the set font from options.
$temp_font_name = get_theme_mod('ounox_fonts_display');
// Adding Font in Google URL
$google_font = esc_url('https://fonts.googleapis.com/css?family=' . $temp_font_name);
// Now Calling google font in website
wp_enqueue_style( 'google-fonts', esc_url_raw($google_font), array(), null );
}
add_action('wp_enqueue_scripts','calling_google_font_script');
推荐阅读
- scala - 从数据框 Scala 中选择列后,列值的顺序会发生变化
- java - examples.Anagram.main(Anagram.java:20) 中的线程“主”java.lang.NullPointerException 中的异常
- node.js - npm call_and_retry_last
- networking - 使用私有 IP 从不同 VPC 网络中的 GKE 集群连接到 Cloud SQL
- php - 在 WooCommerce 订单上使用优惠券代码时发送电子邮件通知
- javascript - Firebase 实时数据库 - Web / Javascript - 如何将多个写入操作作为一个批次执行
- typescript - TypeORM 没有成功连接到我的 Postgres
- selenium - 使用钩子概念无法在其他机器上的范围报告中看到屏幕截图
- mysql - 哪种数据库设计方法最好,这两个选项都有局限性,我找不到解决方案
- ios - 如何从 CVPixelBuffer 中删除 alpha 通道并在 Swift 中获取其数据