get_bloginfo()获取博客站点相关信息

目录

描述

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

get_bloginfo() 描述

用法

 <?php $bloginfo get_bloginfo$show$filter ); ?> 

get_bloginfo() 用法

参数

$show

(string) (可选) Keyword naming the information you want.

默认值: name

  • 'name' - Returns the "Site Title" set in Settings > General. This data is retrieved from the "blogname" record in the wp_options table.
  • 'description' - Returns the "Tagline" set in Settings > General. This data is retrieved from the "blogdescription" record in the wp_options table.
  • 'wpurl' - Returns the "WordPress address (URL)" set in Settings > General. This data is retrieved from the "siteurl" record in the wp_options table. Consider using 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' - Returns the "Site address (URL)" set in Settings > General. This data is retrieved from the "home" record in the wp_options table. Equivalent to home_url().
  • 'admin_email' - Returns the "E-mail address" set in Settings > General. This data is retrieved from the "admin_email" record in the wp_options table.
  • 'charset' - Returns the "Encoding for pages and feeds" set in Settings > Reading. This data is retrieved from the "blog_charset" record in the wp_options table.
  • 'version' - Returns the WordPress Version you use. This data is retrieved from the $wp_version variable set in wp-includes/version.php.
  • 'html_type' - Returns 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' - Returns the Text Direction of WordPress HTML pages. Consider using is_rtl() instead.
  • 'language' - Returns the language of WordPress.
  • 'stylesheet_url' - Returns the primary CSS (usually style.css) file URL of the active theme. Consider using get_stylesheet_uri() instead.
  • 'stylesheet_directory' - Returns the stylesheet directory URL of the active theme. (Was a local path in earlier WordPress versions.) Consider using get_stylesheet_directory_uri() instead.
  • 'template_url' / 'template_directory' - URL of the active theme's directory ('template_directory' was a local path before 2.6; see get_theme_root() and get_template() for hackish alternatives.) Within child themes, both get_bloginfo('template_url') and get_template() will return the parent theme directory. Consider using get_template_directory_uri() instead (for the parent template directory) or get_stylesheet_directory_uri() (for the child template directory).
  • 'pingback_url' - Returns the Pingback XML-RPC file URL (xmlrpc.php).
  • 'atom_url' - Returns the Atom feed URL (/feed/atom).
  • 'rdf_url' - Returns the RDF/RSS 1.0 feed URL (/feed/rfd).
  • 'rss_url' - Returns the RSS 0.92 feed URL (/feed/rss).
  • 'rss2_url' - Returns the RSS 2.0 feed URL (/feed).
  • 'comments_atom_url' - Returns the comments Atom feed URL (/comments/feed).
  • 'comments_rss2_url' - Returns the comments RSS 2.0 feed URL (/comments/feed).
  • 'siteurl' - Deprecated since version 2.2. Use home_url(), or get_bloginfo('url').
  • 'home' - Deprecated since version 2.2. Use home_url(), or get_bloginfo('url').

$filter

(string) (可选) Keyword specifying how to filter what is retrieved.

默认值: raw

  • 'display' - Passes the value of $show through the wptexturize() function before returning it to caller.
  • 'raw' - Returns the value of $show as is.

get_bloginfo() 参数

示例

Default Usage

The default usage assigns your blog's title to the variable $blog_title.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

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

Blog Title

This example assign your blog's title to the variable $blog_title. This returns the same result as the default usage.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

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

Blog Tagline

Using this example:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

 <?php echo 'Your Blog Tagline is: ' . get_bloginfo ( 'description' );  ?><br />

results in this being displayed on your blog:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

 Your Blog Tagline is: All things WordPress

Network Tagline

Using this example, you can obtain the name and description for the network home:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

 <?php
 switch_to_blog(1);
 $site_title = get_bloginfo( 'name' );
 $site_url = network_site_url( '/' );
 $site_description = get_bloginfo( 'description' );
 restore_current_blog();
 echo 'The Network Home URL is: ' . $site_url;
 echo 'The Network Home Name is: ' . $site_title;
 echo 'The Network Home Tagline is: ' . $site_description;  
?>

results in this being displayed on your blog:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

 The Network Home URL is: http://example.com/
 The Network Home Name is: Example
 The Network Home Tagline is: The example site

Template Directory

Returns template directory URL to the active theme.

Example output

From version 2.7. On host example, the Blog address (URL) is shown at http://www.example.com/home, and the WordPress address (URL) is installed at http://www.example.com/home/wp.

Note that directory URLs are missing trailing slashes.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

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() 示例

历史

添加于 版本: 0.71

get_bloginfo() 历史

源文件

get_bloginfo() 函数的代码位于 wp-includes/general-template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve information about the blog.
 *
 * Some show parameter values are deprecated and will be removed in future
 * versions. These options will trigger the {@see _deprecated_argument()}
 * function. The deprecated blog info options are listed in the function
 * contents.
 *
 * The possible values for the 'show' parameter are listed below.
 *
 * 1. url - Blog URI to homepage.
 * 2. wpurl - Blog URI path to WordPress.
 * 3. description - Secondary title
 *
 * The feed URL options can be retrieved from 'rdf_url' (RSS 0.91),
 * 'rss_url' (RSS 1.0), 'rss2_url' (RSS 2.0), or 'atom_url' (Atom feed). The
 * comment feeds can be retrieved from the 'comments_atom_url' (Atom comment
 * feed) or 'comments_rss2_url' (RSS 2.0 comment feed).
 *
 * @since 0.71
 *
 * @global string $wp_version
 *
 * @param string $show   Blog info to retrieve.
 * @param string $filter How to filter what is retrieved.
 * @return string Mostly string values, might be empty.
 */
function get_bloginfo( $show = '', $filter = 'raw' ) {
	switch( $show ) {
		case 'home' : // DEPRECATED
		case 'siteurl' : // DEPRECATED
			_deprecated_argument( __FUNCTION__, '2.2', 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.' ),
				'' . $show . '',
				'bloginfo()',
				'url'
			) );
		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':
			$output = get_locale();
			$output = str_replace('_', '-', $output);
			break;
		case 'text_direction':
			_deprecated_argument( __FUNCTION__, '2.2', 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.' ),
				'' . $show . '',
				'bloginfo()',
				'is_rtl()'
			) );
			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 ) {
			/**
			 * Filter 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 {
			/**
			 * Filter 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;
}

get_bloginfo() 源文件

相关