函数文档

bloginfo()

💡 云策文档标注

概述

bloginfo() 是一个 WordPress 模板函数,用于在模板文件中直接输出当前站点的信息,如站点标题、描述、URL 等。它基于用户配置和设置生成内容,适用于前端显示场景。

关键要点

  • bloginfo() 直接输出信息到浏览器,适用于模板文件中的显示需求。
  • 参数 $show 可选,指定要显示的信息类型,如 'name'(站点标题)、'description'(站点描述)、'url'(站点地址)等。
  • 若需在 PHP 代码中使用这些值作为变量,应使用 get_bloginfo() 函数,因为它返回字符串而非直接输出。
  • 部分参数已弃用(如 'siteurl' 和 'home'),建议使用 home_url() 或 bloginfo('url') 替代。
  • 对于特定场景(如多站点配置),推荐使用更专门的函数,如 site_url()、get_stylesheet_uri() 等。

代码示例

// 输出站点标题
bloginfo('name');

// 输出站点描述
bloginfo('description');

// 错误用法:尝试将输出赋值给变量(返回 null)
$url = bloginfo('url'); // 错误

// 正确用法:使用 get_bloginfo() 获取值
$url = get_bloginfo('url'); // 正确

注意事项

  • bloginfo() 会立即输出结果,不能用于变量赋值;如需在 PHP 逻辑中使用,请调用 get_bloginfo()。
  • 注意参数 'charset' 在 WordPress 3.5 及以后版本中始终输出 'UTF-8',不可配置。
  • 在子主题中,'template_url' 和 'template_directory' 返回父主题目录,需根据需求选择 get_template_directory_uri() 或 get_stylesheet_directory_uri()。

📄 原文内容

Displays information about the current site.

Description

See also

Parameters

$showstringoptional
Site information to display. Default empty.

More Information

Displays information about your site, mostly gathered from the information you supply in your User Profile and General Settings WordPress Administration Screens. It can be used anywhere within a template file. This always prints a result to the browser. If you need the values for use in PHP, use get_bloginfo().

Possible values for $show

  • name‘ – Displays the “Site Title” set in Settings > General. This data is retrieved from the “blogname” record in the wp_options table.
  • description‘ – Displays the “Tagline” set in Settings > General. This data is retrieved from the “blogdescription” record in the wp_options table.
  • wpurl‘ – Displays the “WordPress address (URL)” set in Settings > General. This data is retrieved from the “siteurl” record in the wp_options table. Consider echoing site_url() instead, especially for multi-site configurations using paths instead of subdomains (it will return the root site not the current sub-site).
  • url‘ – Displays the “Site address (URL)” set in Settings > General. This data is retrieved from the “home” record in the wp_options table. Consider echoing home_url() instead.
  • admin_email‘ – Displays the “E-mail address” set in Settings > General. This data is retrieved from the “admin_email” record in the wp_options table.
  • charset‘ – Displays the “Encoding for pages and feeds” set in Settings > Reading. This data is retrieved from the “blog_charset” record in the wp_options table. Note: this parameter always echoes “UTF-8”, which is the default encoding of WordPress.
  • version‘ – Displays the WordPress Version you use. This data is retrieved from the $wp_version variable set in wp-includes/version.php.
  • html_type‘ – Displays the Content-Type of WordPress HTML pages (default: “text/html”). This data is retrieved from the “html_type” record in the wp_options table. Themes and plugins can override the default value using the pre_option_html_type filter.
  • text_direction‘ – Displays the Text Direction of WordPress HTML pages. Consider using is_rtl() instead.
  • language‘ – Displays the language of WordPress.
  • stylesheet_url‘ – Displays the primary CSS (usually style.css) file URL of the active theme. Consider echoing get_stylesheet_uri() instead.
  • stylesheet_directory‘ – Displays the stylesheet directory URL of the active theme. (Was a local path in earlier WordPress versions.) Consider echoing get_stylesheet_directory_uri() instead.
  • template_url‘ / ‘template_directory‘ – URL of the active theme’s directory. Within child themes, both get_bloginfo(‘template_url’) and get_template() will return the parent theme directory. Consider echoing get_template_directory_uri() instead (for the parent template directory) or get_stylesheet_directory_uri() (for the child template directory).
  • pingback_url‘ – Displays the Pingback XML-RPC file URL (xmlrpc.php).
  • atom_url‘ – Displays the Atom feed URL (/feed/atom).
  • rdf_url‘ – Displays the RDF/RSS 1.0 feed URL (/feed/rfd).
  • rss_url‘ – Displays the RSS 0.92 feed URL (/feed/rss).
  • rss2_url‘ – Displays the RSS 2.0 feed URL (/feed).
  • comments_atom_url‘ – Displays the comments Atom feed URL (/comments/feed).
  • comments_rss2_url‘ – Displays the comments RSS 2.0 feed URL (/comments/feed).
  • siteurl‘ – Deprecated since version 2.2. Echo home_url() , or use bloginfo(‘url’).
  • home‘ – Deprecated since version 2.2. Echo home_url() , or use bloginfo(‘url’).

Source

function bloginfo( $show = '' ) {
	echo get_bloginfo( $show, 'display' );
}

Changelog

Version Description
0.71 Introduced.

User Contributed Notes

  1. Skip to note 15 content

    Example output

    In this example case, the Site Address (URL) is shown as http://www.example.com/home, and the WordPress address (URL) is displayed as http://www.example.com/home/wp.

    Please note that directory URLs are missing trailing slashes.

    admin_email          = <a href="mailto:admin@example.com">admin@example.com</a>
    atom_url             = <a href="http://www.example.com/home/feed/atom" rel="nofollow ugc">http://www.example.com/home/feed/atom</a>
    charset              = UTF-8
    comments_atom_url    = <a href="http://www.example.com/home/comments/feed/atom" rel="nofollow ugc">http://www.example.com/home/comments/feed/atom</a>
    comments_rss2_url    = <a href="http://www.example.com/home/comments/feed" rel="nofollow ugc">http://www.example.com/home/comments/feed</a>
    description          = Just another WordPress blog
    home                 = <a href="http://www.example.com/home" rel="nofollow ugc">http://www.example.com/home</a> (DEPRECATED! use url option instead)
    html_type            = text/html
    language             = en-US
    name                 = Testpilot
    pingback_url         = <a href="http://www.example.com/home/wp/xmlrpc.php" rel="nofollow ugc">http://www.example.com/home/wp/xmlrpc.php</a>
    rdf_url              = <a href="http://www.example.com/home/feed/rdf" rel="nofollow ugc">http://www.example.com/home/feed/rdf</a>
    rss2_url             = <a href="http://www.example.com/home/feed" rel="nofollow ugc">http://www.example.com/home/feed</a>
    rss_url              = <a href="http://www.example.com/home/feed/rss" rel="nofollow ugc">http://www.example.com/home/feed/rss</a>
    siteurl              = <a href="http://www.example.com/home" rel="nofollow ugc">http://www.example.com/home</a> (DEPRECATED! use url option instead)
    stylesheet_directory = <a href="http://www.example.com/home/wp/wp-content/themes/largo" rel="nofollow ugc">http://www.example.com/home/wp/wp-content/themes/largo</a>
    stylesheet_url       = <a href="http://www.example.com/home/wp/wp-content/themes/largo/style.css" rel="nofollow ugc">http://www.example.com/home/wp/wp-content/themes/largo/style.css</a>
    template_directory   = <a href="http://www.example.com/home/wp/wp-content/themes/largo" rel="nofollow ugc">http://www.example.com/home/wp/wp-content/themes/largo</a>
    template_url         = <a href="http://www.example.com/home/wp/wp-content/themes/largo" rel="nofollow ugc">http://www.example.com/home/wp/wp-content/themes/largo</a>
    text_direction       = ltr
    url                  = <a href="http://www.example.com/home" rel="nofollow ugc">http://www.example.com/home</a>
    version              = 3.5
    wpurl                = <a href="http://www.example.com/home/wp" rel="nofollow ugc">http://www.example.com/home/wp</a>