get_bloginfo (获取网站信息)

描述

通过特定的参数来返回当前网站的信息,和 bloginfo() 一样,可以用来在模板文件的任何地方显示你博客的信息。

语法

get_bloginfo( string $show = '', string $filter = 'raw' )

参数

$show:(string) (Optional) 默认为空,此时返回博客名

  • 'name' – 网站标题 (设置 > 常规)
  • 'description' – 网站标语 (设置 > 常规)
  • 'wpurl' – WordPress地址 (设置 > 常规)
  • 'url' – 网址 (设置 > 常规)
  • 'admin_email' – 电子邮件地址 (设置 > 常规)
  • 'charset' – 编码
  • 'version' –WordPress版本
  • 'html_type' – content-type (默认: "text/html")。主题或插件可以通过 'pre_option_html_type'过滤器来更改
  • 'text_direction' – 由站点语言决定的文字方向.
  • 'language' – 站点语言
  • 'stylesheet_url' – 当前主题css样式表的url地址
  • 'stylesheet_directory' – 当前主题的目录url地址
  • 'template_url' / 'template_directory' – 当前主题目录的url网址
  • 'pingback_url' – XML-RPC(xmlrpc.php) URL
  • 'atom_url' – Atom feed (/feed/atom) URL
  • 'rdf_url' – RDF/RSS 1.0 feed (/feed/rfd) URL
  • 'rss_url' – RSS 0.92 feed (/feed/rss) URL
  • 'rss2_url' – RSS 2.0 feed (/feed) url URL
  • 'comments_atom_url' – 评论 Atom feed URL (/comments/feed)
  • 'comments_rss2_url' – 评论 RSS 2.0 feed URL (/comments/feed)

建议放弃使用的参数:

  • 'siteurl' – 使用 'url' 代替
  • 'home' – 使用'url' 代替

$filter:(string) (Optional) 返回结果的过滤器 默认为 'raw'

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

返回值

一般为string,可能为空

//各参数输出示例 注意下面的目录 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

示例

默认情况下,获取标题。

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

获取标题,输出结果和上面的默认用法一样。

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

获取博客副标题

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

源码

位置:wp-includes/general-template.php

function get_bloginfo( $show = '', $filter = 'raw' ) {
    switch( $show ) {
        case 'home' : // DEPRECATED
        case 'siteurl' : // DEPRECATED
            _deprecated_argument( __FUNCTION__, '2.2.0', sprintf(
                /* translators: 1: 'siteurl'/'home' argument, 2: bloginfo() function name, 3: 'url' argument */
                __( 'The %1$s option is deprecated for the family of %2$s functions. Use the %3$s option instead.' ),
                '<code>' . $show . '</code>',
                '<code>bloginfo()</code>',
                '<code>url</code>'
            ) );
        case 'url' :
            $output = home_url();
            break;
        case 'wpurl' :
            $output = site_url();
            break;
        case 'description':
            $output = get_option('blogdescription');
            break;
        case 'rdf_url':
            $output = get_feed_link('rdf');
            break;
        case 'rss_url':
            $output = get_feed_link('rss');
            break;
        case 'rss2_url':
            $output = get_feed_link('rss2');
            break;
        case 'atom_url':
            $output = get_feed_link('atom');
            break;
        case 'comments_atom_url':
            $output = get_feed_link('comments_atom');
            break;
        case 'comments_rss2_url':
            $output = get_feed_link('comments_rss2');
            break;
        case 'pingback_url':
            $output = site_url( 'xmlrpc.php' );
            break;
        case 'stylesheet_url':
            $output = get_stylesheet_uri();
            break;
        case 'stylesheet_directory':
            $output = get_stylesheet_directory_uri();
            break;
        case 'template_directory':
        case 'template_url':
            $output = get_template_directory_uri();
            break;
        case 'admin_email':
            $output = get_option('admin_email');
            break;
        case 'charset':
            $output = get_option('blog_charset');
            if ('' == $output) $output = 'UTF-8';
            break;
        case 'html_type' :
            $output = get_option('html_type');
            break;
        case 'version':
            global $wp_version;
            $output = $wp_version;
            break;
        case 'language':
            /* translators: Translate this to the correct language tag for your locale,
             * see https://www.w3.org/International/articles/language-tags/ for reference.
             * Do not translate into your own language.
             */
            $output = __( 'html_lang_attribute' );
            if ( 'html_lang_attribute' === $output || preg_match( '/[^a-zA-Z0-9-]/', $output ) ) {
                $output = get_locale();
                $output = str_replace( '_', '-', $output );
            }
            break;
        case 'text_direction':
            _deprecated_argument( __FUNCTION__, '2.2.0', sprintf(
                /* translators: 1: 'text_direction' argument, 2: bloginfo() function name, 3: is_rtl() function name */
                __( 'The %1$s option is deprecated for the family of %2$s functions. Use the %3$s function instead.' ),
                '<code>' . $show . '</code>',
                '<code>bloginfo()</code>',
                '<code>is_rtl()</code>'
            ) );
            if ( function_exists( 'is_rtl' ) ) {
                $output = is_rtl() ? 'rtl' : 'ltr';
            } else {
                $output = 'ltr';
            }
            break;
        case 'name':
        default:
            $output = get_option('blogname');
            break;
    }
 
    $url = true;
    if (strpos($show, 'url') === false &&
        strpos($show, 'directory') === false &&
        strpos($show, 'home') === false)
        $url = false;
 
    if ( 'display' == $filter ) {
        if ( $url ) {
            /**
             * Filters the URL returned by get_bloginfo().
             *
             * @since 2.0.5
             *
             * @param mixed $output The URL returned by bloginfo().
             * @param mixed $show   Type of information requested.
             */
            $output = apply_filters( 'bloginfo_url', $output, $show );
        } else {
            /**
             * Filters the site information returned by get_bloginfo().
             *
             * @since 0.71
             *
             * @param mixed $output The requested non-URL site information.
             * @param mixed $show   Type of information requested.
             */
            $output = apply_filters( 'bloginfo', $output, $show );
        }
    }
 
    return $output;
}