display_header()
概述
display_header() 函数用于显示 WordPress 安装界面的头部内容,包括设置 HTML 字符集和根据语言方向添加 RTL 类。
关键要点
- 函数接受一个可选的字符串参数 $body_classes,用于向 body 标签添加自定义 CSS 类。
- 自动检测当前语言是否为 RTL(从右到左),如果是,则向 body 类添加 'rtl'。
- 函数内部设置 Content-Type 为 text/html; charset=utf-8,确保正确编码。
代码示例
function display_header( $body_classes = '' ) {
header( 'Content-Type: text/html; charset=utf-8' );
if ( is_rtl() ) {
$body_classes .= 'rtl';
}
if ( $body_classes ) {
$body_classes = ' ' . $body_classes;
}
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php wp_admin_css( 'install' ); ?>
<?php _e( 'WordPress Installation' ); ?>
</head>
<body class="<?php echo $body_classes; ?>">
<div id="header">
<h1><?php _e( 'WordPress' ); ?></h1>
</div>
</body>
</html>
<?php
}注意事项
- 此函数主要用于 WordPress 安装过程,不应在常规主题或插件开发中直接调用。
- 参数 $body_classes 应以空格分隔多个类名,函数会自动在开头添加空格。
- 依赖 language_attributes()、wp_admin_css()、is_rtl() 和 _e() 等辅助函数来正确渲染头部。
Display installation header.
Parameters
$body_classesstringrequired
Source
function display_header( $body_classes = '' ) {
header( 'Content-Type: text/html; charset=utf-8' );
if ( is_rtl() ) {
$body_classes .= 'rtl';
}
if ( $body_classes ) {
$body_classes = ' ' . $body_classes;
}
?>
<html <?php language_attributes(); ?>>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="noindex,nofollow" />
<title></title>
</head>
<body class="wp-core-ui<?php echo $body_classes; ?>">
<p id="logo"></p>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/install.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/install.php#L57">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/install.php#L57-L79">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/language_attributes/">language_attributes()</a><code>wp-includes/general-template.php
Displays the language attributes for the ‘html’ tag.
wp_admin_css()wp-includes/general-template.php
Enqueues or directly prints a stylesheet link to the specified CSS file.
is_rtl()wp-includes/l10n.php
Determines whether the current locale is right-to-left (RTL).
_e()wp-includes/l10n.php
Displays translated text.
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |