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.
...$0arrayAn indexed or associative (keyed by font-family) array of font variations for this font-family.
Each font face has the following structure....$0arrayThe font face properties.font-familystringThe font-family property.srcstring|string[]The URL(s) to each resource containing the font data.font-stylestringOptional. The font-style property. Default'normal'.font-weightstringOptional. The font-weight property. Default'400'.font-displaystringOptional. The font-display property. Default'fallback'.ascent-overridestringOptional. The ascent-override property.descent-overridestringOptional. The descent-override property.font-stretchstringOptional. The font-stretch property.font-variantstringOptional. The font-variant property.font-feature-settingsstringOptional. The font-feature-settings property.font-variation-settingsstringOptional. The font-variation-settings property.line-gap-overridestringOptional. The line-gap-override property.size-adjuststringOptional. The size-adjust property.unicode-rangestringOptional. 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
You must log in before being able to contribute a note or feedback.
Skip to note 2 content
Rodrigo Vieira Eufrasio da Silva
$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 );