函数文档

wp_print_font_faces()

💡 云策文档标注

概述

wp_print_font_faces() 函数用于生成并打印指定字体或 theme.json 中定义的字体对应的 @font-face 样式。它接受一个字体数组参数,若无参数则自动从 theme.json 解析字体。

关键要点

  • 函数参数 $fonts 为可选数组,可包含字体族及其变体的详细属性,如 font-family、src、font-style、font-weight 等。
  • 若 $fonts 为空,函数会调用 WP_Font_Face_Resolver::get_fonts_from_theme_json() 从 theme.json 获取字体数据。
  • 函数内部实例化 WP_Font_Face 类并调用 generate_and_print() 方法生成和输出 CSS 样式。
  • 此函数自 WordPress 6.4.0 版本引入,用于处理字体资源加载。

代码示例

$fonts = array(
    'font-family-1' => array(
        'variations' => array(
            'font-variation-1' => array(
                'font-family' => 'Font Family 1',
                'src' => array(
                    'url-to-font-variation-1.woff',
                    'url-to-font-variation-1.woff2',
                ),
                'font-style' => 'italic',
                'font-weight' => '700',
                // Other optional properties...
            ),
            // Add more variations if necessary...
        ),
    ),
    'font-family-2' => array(
        'variations' => array(
            'font-variation-2' => array(
                'font-family' => 'Font Family 2',
                'src' => array(
                    'url-to-font-variation-2.woff',
                    'url-to-font-variation-2.woff2',
                ),
                // Outras propriedades opcionais...
            ),
            // Add more variations if necessary...
        ),
    ),
    // Add more font families if necessary...
);

wp_print_font_faces( $fonts );

📄 原文内容

Generates and prints font-face styles for given fonts or theme.json fonts.

Parameters

$fontsarray[][]optional
The font-families and their font faces.

  • ...$0 array
    An indexed or associative (keyed by font-family) array of font variations for this font-family.
    Each font face has the following structure.

    • ...$0 array
      The font face properties.

      • font-family string
        The font-family property.
      • src string|string[]
        The URL(s) to each resource containing the font data.
      • font-style string
        Optional. The font-style property. Default 'normal'.
      • font-weight string
        Optional. The font-weight property. Default '400'.
      • font-display string
        Optional. The font-display property. Default 'fallback'.
      • ascent-override string
        Optional. The ascent-override property.
      • descent-override string
        Optional. The descent-override property.
      • font-stretch string
        Optional. The font-stretch property.
      • font-variant string
        Optional. The font-variant property.
      • font-feature-settings string
        Optional. The font-feature-settings property.
      • font-variation-settings string
        Optional. The font-variation-settings property.
      • line-gap-override string
        Optional. The line-gap-override property.
      • size-adjust string
        Optional. The size-adjust property.
      • unicode-range string
        Optional. The unicode-range property.
        }

    Default:array()

Source

function wp_print_font_faces( $fonts = array() ) {

	if ( empty( $fonts ) ) {
		$fonts = WP_Font_Face_Resolver::get_fonts_from_theme_json();
	}

	if ( empty( $fonts ) ) {
		return;
	}

	$wp_font_face = new WP_Font_Face();
	$wp_font_face->generate_and_print( $fonts );
}

Changelog

Version Description
6.4.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    $fonts = array(
        'font-family-1' => array(
            'variations' => array(
                'font-variation-1' => array(
                    'font-family' => 'Font Family 1',
                    'src' => array(
                        'url-to-font-variation-1.woff',
                        'url-to-font-variation-1.woff2',
                    ),
                    'font-style' => 'italic',
                    'font-weight' => '700',
                    // Other optional properties...
                ),
                // Add more variations if necessary...
            ),
        ),
        'font-family-2' => array(
            'variations' => array(
                'font-variation-2' => array(
                    'font-family' => 'Font Family 2',
                    'src' => array(
                        'url-to-font-variation-2.woff',
                        'url-to-font-variation-2.woff2',
                    ),
                    // Outras propriedades opcionais...
                ),
                // Add more variations if necessary...
            ),
        ),
        // Add more font families if necessary...
    );
    
    wp_print_font_faces( $fonts );

You must log in before being able to contribute a note or feedback.