函数原型:
wp_enqueue_block_style( string $block_name, array $args )
为特定块引入样式表
如果主题选择了单独加载样式,那么样式表将在渲染时排队,否则在块初始化时排队。
参数说明:
$block_name 块名称,包括命名空间
$args 包括下面字段:
handle 样式表的句柄
src 样式表的源URL
deps 已注册样式表数组处理此样式表依赖于
ver 样式表版本号
media 已为其定义此样式表的媒体
path 样式表的绝对路径,因此它可能被内联
函数源码:
function wp_enqueue_block_style( $block_name, $args ) {
$args = wp_parse_args(
$args,
array(
'handle' => '',
'src' => '',
'deps' => array(),
'ver' => false,
'media' => 'all',
)
);
$callback = static function ( $content ) use ( $args ) {
// Register the stylesheet.
if ( ! empty( $args['src'] ) ) {
wp_register_style( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['media'] );
}
// Add `path` data if provided.
if ( isset( $args['path'] ) ) {
wp_style_add_data( $args['handle'], 'path', $args['path'] );
// Get the RTL file path.
$rtl_file_path = str_replace( '.css', '-rtl.css', $args['path'] );
// Add RTL stylesheet.
if ( file_exists( $rtl_file_path ) ) {
wp_style_add_data( $args['handle'], 'rtl', 'replace' );
if ( is_rtl() ) {
wp_style_add_data( $args['handle'], 'path', $rtl_file_path );
}
}
}
// Enqueue the stylesheet.
wp_enqueue_style( $args['handle'] );
return $content;
};
$hook = did_action( 'wp_enqueue_scripts' ) ? 'wp_footer' : 'wp_enqueue_scripts';
if ( wp_should_load_separate_core_block_assets() ) {
$callback_separate = static function ( $content, $block ) use ( $block_name, $callback ) {
if ( ! empty( $block['blockName'] ) && $block_name === $block['blockName'] ) {
return $callback( $content );
}
return $content;
};
add_filter( 'render_block', $callback_separate, 10, 2 );
return;
}
add_filter( $hook, $callback );
// Enqueue assets in the editor.
add_action( 'enqueue_block_assets', $callback );
}
使用举例:
function twentytwentytwo_support() {
/*
* Load additional block styles.
*/
$styled_blocks = ['post-author'];
foreach ( $styled_blocks as $block_name ) {
$args = array(
'handle' => "twentytwentytwo-$block_name",
'src' => get_theme_file_uri( "assets/css/blocks/$block_name.css" ),
);
wp_enqueue_block_style( "core/$block_name", $args );
}
}
-
WP Multilang WordPress翻译插件WP Multilang插件的主要功能是提供多语言支持,使用户能够创建多语言版本的网站,满足不同语言用户的需求。
-
Post Type Switcher将 WordPress文章从一个类型转换到另一个类型Post Type Switcher是一个WordPress插件,用于将文章从一个类型转换到另一个类型,包括自定义文章类型。安装并启用该插件后,在编辑文章时,会在发布面板中增加Post Type选项,用户可以选择要转换到的文章类型并更新文章。此外,插件也支持在后台所有文章页面批量更改文章类型。
-
WordPress公司官网主题在众多的WordPress企业官网主题中,追格公司推出了多款关于WordPress企业官网主题作品。我们之前已经分享过一些关于追格的企业官网主题,包括收费和免费版本,这些主题都因其独特的设计和出色的功能而深受用户喜爱。
-
Fluent Forms - WordPress表单插件Fluent Forms是一款功能强大且易于使用的WordPress表单插件,适合各种规模的网站使用。无论是简单的联系表单还是复杂的用户调查表,Fluent Forms都能满足用户的需求。
-
WP-Ban WordPress禁止指定IP访问网站的插件WP-Ban是一款专为WordPress网站设计的安全插件,其主要功能是屏蔽恶意或不受欢迎的IP地址,以增强网站的安全性。
-
WPvivid插件:WordPress网站备份与还原的解决方案WPvivid插件是一款功能强大的WordPress插件,专为网站数据备份、还原和搬家设计。它支持高度自定义的备份选项,允许用户选择备份整个站点(包括数据库和文件)、仅文件或仅数据库。
暂无评论,抢个沙发...