首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress主题开发与Google

Wordpress主题开发与Google
EN

Stack Overflow用户
提问于 2018-06-13 19:04:20
回答 1查看 308关注 0票数 0

我正在开发一个主题,我得到了这段代码,它使google字体下拉。下拉列表工作得很好,但是我的问题是,我似乎可以让它在我的css中工作。

代码语言:javascript
复制
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应用于我的主题。

代码语言:javascript
复制
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?抱歉,后端的事情真糟糕。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-13 19:41:30

在functions.php中添加以下函数

代码语言:javascript
复制
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');
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50844508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档