描述

返回你博客的信息,这些信息可以用在任何地方的 PHP 代码中。这个函数,和 bloginfo() 一样,可以用来在模板文件的任何地方显示你博客的信息。

用法

<?php $bloginfo = get_bloginfo( $show, $filter ); ?>

参数

$show(字符串 string) (可选) 关键字命名你想要的信息。

默认: name

'name' - 返回在 设置 > 常规 中设置的“博客标题”。此数据可以从 wp_options 这个表中的 “blogname”中检索到。

'description' - 返回在 设置 > 常规 中设置的“副标题”。此数据可以从 wp_options 这个表中的 “blogdescription”中检索到。

'wpurl' - 返回在 设置 > 常规 中设置的“WordPress 地址 (URL)”。此数据可以从 wp_options 这个表中的 “siteurl”中检索到。 可以考虑使用 site_url() 替代, 尤其是在使用 子目录路径方式,而不是使用 子域名 来配置多站点时(bloginfo将返回根网站的URL,而不是子站点的URL)。

'siteurl' / 'url' - 显示在 设置 > 常规 中设置的 “站点地址(URL)”)”。该数据是从 wp_options 这个数据表中检索到的 "home"记录。 可以考虑使用 home_url() 代替。

'admin_email' - 显示在 设置 > 常规 中设置的 “电子邮件地址”。该数据是从 wp_options 这个数据表中检索到的 "admin_email"记录。

'charset' - 显示在 设置 > 常规 中设置的“页面和feed的编码”。该数据是从 wp_options 这个数据表中检索到的"blog_charset" 记录。(注:3.5.1+好像已经没有这个选项了)

'version' - 显示你当前使用的 WordPress 版本。该数据是在 wp-includes/version.php 检索到的 $wp_version  这个字段的值。

'html_type' - 显示WordPress HTML 页面中的内容类型(默认: "text/html")。该数据可以从 wp_options 这个数据表中检索到的 "html_type" 记录。主题和插件可以通过使用 pre_option_html_type 过滤器覆盖默认值。

'text_direction' - 显示 WordPress HTML 页面的文本方向。可以考虑使用 is_rtl() 代替。

'language' - 显示WordPress的语言。

'stylesheet_url' - 显示当前使用的主题的 CSS文件(通常为 style.css)路径。可以考虑使用 get_stylesheet_uri() 代替。

'stylesheet_directory' - 显示当前使用的主题的样式表路径。可以考虑使用 get_stylesheet_directory_uri() 代替。

'template_url' / 'template_directory' - 当前主题的 URL 路径 。在子主题中, get_bloginfo('template_url') 和 get_template() 都将返回父主题的目录。可以考虑使用 get_template_directory_uri() (用于父主题目录)或get_stylesheet_directory_uri()(用于子主题目录)代替。

'pingback_url' - 显示通告文件 XML-RPC 的URL (xmlrpc.php)

'atom_url' - 显示 Atom feed URL (/feed/atom)

'rdf_url' - 显示 RDF/RSS 1.0 feed URL (/feed/rfd).

'rss_url' - 显示 RSS 0.92 feed URL (/feed/rss).

'rss2_url' - 显示 RSS 2.0 feed URL (/feed).

'comments_atom_url' - 显示评论的 Atom feed URL (/comments/feed).

'comments_rss2_url' - 显示评论的 RSS 2.0 feed URL (/comments/feed).

$filter(字符串 string) (可选)关键字指定如何过滤检索到的数据。

默认: raw

'display' - 通过 wptexturize() 函数过滤 $show 的值,然后返回输出给请求者。

'raw' - 直接返回 $show 的值。

例子

默认用法

默认情况下,将你的博客标题定义给 $blog_title 变量。

<?php $blog_title = get_bloginfo(); ?>

博客标题

下面的例子将你的博客标题定义给 $blog_title 变量,输出结果和上面的默认用法一样。

<?php $blog_title = get_bloginfo('name'); ?>

博客副标题

使用下面的例子:

<?php echo '你博客的副标题是: ' . get_bloginfo ( 'description' );  ?>

返回现实的内容是:

你博客的副标题是: All things WordPress

网络副标题(多站点)

使用下面的代码,你可以得到网络首页的名称和副标题:

 <?php
 switch_to_blog(1);
 $site_title = get_bloginfo( 'name' );
 $site_url = network_site_url( '/' );
 $site_description = get_bloginfo( 'description' );
 restore_current_blog();
 echo '网络首页的 URL 是: ' . $site_url;
 echo '网络首页的 名称 是: ' . $site_title;
 echo '网络首页的 副标题 是: ' . $site_description;  
?>

返回的输出结果如下:

网络首页的 URL 是: http://example.com/
网络首页的 名称 是: Example
网络首页的 副标题 是: The example site

模板目录

返回当前模板目录的 URL

示例输出

从 2.7 版本以来,在主机的例子, Blog address (URL) 显示在http://www.example.com/home,WordPress address (URL) 安装在http://www.example.com/home/wp。

注意:下面的目录 URL 末尾都是不带斜线的。

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

所在文件

get_bloginfo() 包含在 wp-includes/general-template.php.

相关函数

bloginfo()

get_admin_url()

原文:http://codex.wordpress.org/Function_Reference/get_bloginfo