【问题标题】:WordPress - Google Fonts not working in .php fileWordPress - Google 字体在 .php 文件中不起作用
【发布时间】:2021-04-08 18:32:12
【问题描述】:

我使用“WooCommerce PRO 的灵活发票”插件,但遇到了问题。基于 .php 文件,生成一个 .pdf 文件。 我在 .php 文件中添加了 Poppins 字体的链接,但是在网页上打开文件后,字体不起作用。最终,它应该是一个pdf文件。 我尝试了不同的点:

  1. 我添加到部分和容器中
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@100;300;400;500;600;700;900&display=swap" rel="stylesheet">
body, p, tr, th, td {font-family: 'Poppins', sans-serif!important;}
  1. 我使用了@import 选项:
@import 'https://fonts.googleapis.com/css2?family=Poppins:wght@100;300;400;500;600;700;900&display=swap';
  1. 我使用了一个单独的 .css 文件和 @media print {}
  2. 我在functions.php文件中添加了一个函数
function wpb_add_google_fonts() {
   wp_enqueue_style( 'wpb-google-fonts', 'https://fonts.googleapis.com/css2?family=Poppins:wght@100;300;400;500;600;700;900&display=swap', false );
   }
   
   add_action( 'wp_enqueue_scripts', 'wpb_add_google_fonts' );

很遗憾,Poppins 字体仍然无法使用。您对如何解决它有任何想法吗?

【问题讨论】:

    标签: javascript php jquery css wordpress


    【解决方案1】:

    一切都在我身边。 Poppins 正确渲染。这是我测试过的。

    <?php
    
    /**
     * Enqueue Poppins font from Google Fonts.
     *
     * @link https://fonts.google.com/specimen/Poppins?query=Poppins#about
     */
    wp_enqueue_style( 'google_fonts', 'https://fonts.googleapis.com/css2?family=Poppins:wght@300&display=swap', array(), wp_get_theme()->version, 'all' );
    
    add_action( 'style_loader_tag', 'style_loader_tag_google_fonts', 10, 3 );
    if ( ! function_exists( 'style_loader_tag_google_fonts' ) ) {
        function style_loader_tag_google_fonts( $tag, $handle, $src ) {
            if ( 'google_fonts' === $handle ) {
                $tag = str_replace(
                    "<link rel='stylesheet'", 
                    "<link rel='preconnect' href='" . esc_url( 'https://fonts.gstatic.com' ) . "' /><link rel='stylesheet'", 
                    $tag
                );
            };
            return $tag;
        };
    };
    

    您也可以直接从标签中指定media="print"

    //... array(), wp_get_theme()->version, 'print' );
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-25
    • 2014-04-03
    • 2015-10-20
    • 2022-01-07
    • 2012-02-21
    • 2014-10-17
    • 2014-09-19
    • 2016-11-27
    相关资源
    最近更新 更多