返回介绍

is_home()

发布于 2017-09-11 01:16:27 字数 5040 浏览 1329 评论 0 收藏 0

is_home()

Determines if the query is for the blog homepage.


description

The blog homepage is the page that shows the time-based blog content of the site.

is_home() is dependent on the site’s "Front page displays" Reading Settings ‘show_on_front’ and ‘page_for_posts’.

If a static page is set for the front page of the site, this function will return true only on the page you set as the "Posts page".


返回值

(bool) True if blog view homepage, otherwise false.


源代码

File: wp-includes/query.php

function is_home() {
	global $wp_query;

	if ( ! isset( $wp_query ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
		return false;
	}

	return $wp_query->is_home();
}

更新日志

Versiondescription
1.5.0Introduced.

More Information

History

Since WordPress 2.1, when the static front page functionality was introduced, the blog posts index and site front page have been treated as two different query contexts, with is_home() applying to the blog posts index, and is_front_page() applying to the site front page.

Usage

Be careful not to confuse the two query conditionals:

  • On the site front page, is_front_page() will always return true, regardless of whether the site front page displays the blog posts index or a static page.
  • On the blog posts index, is_home() will always return true, regardless of whether the blog posts index is displayed on the site front page or a separate page.

Whether is_home() or is_front_page() return true or false depends on the values of certain option values:

  • get_option( 'show_on_front' ): returns either 'posts' or 'page'
  • get_option( 'page_on_front' ): returns the ID of the static page assigned to the front page
  • get_option( 'page_for_posts' ): returns the ID of the static page assigned to the blog posts index (posts page)

When using these query conditionals:

  • If 'posts' == get_option( 'show_on_front' ):
    • On the site front page:
      • is_front_page() will return true
      • is_home() will return true
    • If assigned, WordPress ignores the pages assigned to display the site front page or the blog posts index
  • If 'page' == get_option( 'show_on_front' ):
    • On the page assigned to display the site front page:
      • is_front_page() will return true
      • is_home() will return false
    • On the page assigned to display the blog posts index:
      • is_front_page() will return false
      • is_home() will return true

Notes

is_home() uses the global $wp_query WP_Query object. is_home() isn’t usable before the ‘parse_query’ action.


相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/class-wp-query.php: WP_Query::is_home()
  • wp-includes/functions.php: _doing_it_wrong()

Used By

  • wp-includes/general-template.php: wp_get_document_title()
  • wp-includes/general-template.php: wp_title()
  • wp-includes/class-wp.php: WP::handle_404()
  • wp-includes/post-template.php: get_post_class()
  • wp-includes/post-template.php: get_body_class()
  • wp-includes/canonical.php: redirect_canonical()
  • Show 1 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: -1You must log in to vote on the helpfulness of this note Contributed by znowebdev

    The following example can be used in your sidebar to display different content when displaying the blog posts index.

    
    if ( is_home() ) {
        // This is the blog posts index
        get_sidebar( 'blog' );
    } else {
        // This is not the blog posts index
        get_sidebar();
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文