WordPress is_home () Judge Invalid solution

Source: Internet
Author: User
The code is as follows Copy Code

<?php if (Is_home ()) {?>
I will only show on the front page
<?php}?>


Is_home (); function returns a true when it is on the first page
The above code is commonly used only on the first page display
Insert any template page PHP can,

such as sidebar sidebar.php or footer footer.php

But if it had been preceded by a query_posts (); It's going to invalidate it.
The reason is that is_home is_virgin Is_ooxx this is_ prefix is based on the theme loop,
and Query_posts (); It's going to deviate from the main loop.

The solution is in is_home (); Before adding a wp_reset_query ();

The code is as follows Copy Code

<?php Wp_reset_query (); if (Is_home ()) {?>
I will only show on the homepage, really!
<?php}?>


Second Kind
When you use the specified page as a page. In this case is_home () is not working.

You can use Is_front_page () to determine whether the current page is a specified homepage, and we need this function in the case described above.

  code is as follows copy code

<?php if (Is_home () | | | is_front_page ()) The
Home page shows the code ................
<?php}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.