_custom_logo_header_styles()
概述
此函数根据自定义器设置,为自定义徽标添加CSS以隐藏标题文本。它检查主题是否支持自定义标题和自定义徽标,并基于主题修改值动态生成CSS类。
关键要点
- 函数 _custom_logo_header_styles() 用于在特定条件下输出CSS样式,以隐藏与自定义徽标相关的标题文本。
- 条件检查包括:主题不支持 custom-header 的 header-text、主题支持 custom-logo 的 header-text,且主题修改 header_text 为 false。
- 使用 get_theme_support()、get_theme_mod() 和 current_theme_supports() 函数来获取主题支持和设置。
- 生成的CSS类通过 sanitize_html_class() 进行清理,并输出为内联样式标签。
代码示例
function _custom_logo_header_styles() {
if ( ! current_theme_supports( 'custom-header', 'header-text' )
&& get_theme_support( 'custom-logo', 'header-text' )
&& ! get_theme_mod( 'header_text', true )
) {
$classes = (array) get_theme_support( 'custom-logo', 'header-text' );
$classes = array_map( 'sanitize_html_class', $classes );
$classes = '.' . implode( ', .', $classes );
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
?>
<style<?php echo $type_attr; ?>>
<?php echo $classes; ?> {
position: absolute;
clip: rect(1px, 1px, 1px, 1px);
}
</style>
<?php
}
} Adds CSS to hide header text for custom logo, based on Customizer setting.
Source
function _custom_logo_header_styles() {
if ( ! current_theme_supports( 'custom-header', 'header-text' )
&& get_theme_support( 'custom-logo', 'header-text' )
&& ! get_theme_mod( 'header_text', true )
) {
$classes = (array) get_theme_support( 'custom-logo', 'header-text' );
$classes = array_map( 'sanitize_html_class', $classes );
$classes = '.' . implode( ', .', $classes );
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
?>
<!-- Custom Logo: hide header text -->
<style id="custom-logo-css"<?php echo $type_attr; ?>>
<?php echo $classes; ?> {
position: absolute;
clip-path: inset(50%);
}
</style>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-includes/theme.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/theme.php#L3001">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/theme.php#L3001-L3021">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/get_theme_support/">get_theme_support()</a><code>wp-includes/theme.php
Gets the theme support arguments passed when registering that support.
get_theme_mod()wp-includes/theme.php
Retrieves theme modification value for the active theme.
current_theme_supports()wp-includes/theme.php
Checks a theme’s support for a given feature.
Changelog
| Version | Description |
|---|---|
| 4.5.0 | Introduced. |