WordPress.org

Ready to get started?Download WordPress

Forums

is_home() and is_front_page() known to be borked? (14 posts)

  1. RonJeffries
    Member
    Posted 5 years ago #

    My footer.php wants to include some checks to see whether to display some sponsor lines, which depend on whether the (web) page is the home page, or one of a small set of (wp) pages, or a specific (wp) post.

    The if statement provided to detect the home page does not trigger with is_home(), nor with is_front_page, nor with is_page(<anything I could think of>) nor with is_single(<ditto.).

    I have finally set a global in homepage.php and checked it in the footer. In the course of figuring this hack out, I tweeted and chatted with pals who know WP.

    The general impression is that is_home() and is_front_page() are known to be borked. Is this the case? Is there a plan? Is there a good workaround?

    Thanks!

    Ron

  2. Mark / t31os
    Moderator
    Posted 5 years ago #

    I've just gone and added the following into the footer.php of the theme on my local install..

    <?php if(is_home() || is_front_page()) echo 'match'; ?>

    Worked first time...

    Perhaps if you could post the code you use we can see where the problem lies...

  3. RonJeffries
    Member
    Posted 5 years ago #

    The code in footer is just about like that, although I tested things one at a time.

    The issue, I believe, is that I have a very complex static page as home page, which includes two instances of the loop, listing articles, plus a link to a full index page. What seems to be happening is that by the time one gets to footer, all the "what page is this" stuff has been clobbered by the last article that happens to show up in the index.

    I could post the whole shebang but not at all sure it would help.

    What I found was that if (TRUE) would of course print the ads.
    if ( is_home() ) would not, nor would if ( is_front_page() ), nor would if (is_single('XProgramming.com') ) (the name of the page).

    What /did/ work (but was wrong) was if ( is_page('Rails ...') ), which is the name of the page last in the index loop, or is_page('691'), that page's ID. That was, of course, useless.

    Anyway, the global works, and isn't entirely hideous. I will look at whether I should create some special tags or categories for these special pages.

    Thanks,

  4. Mark / t31os
    Moderator
    Posted 5 years ago #

    Can i see the code that relates to both your loops?....

    ie. the query_posts() parts etc...

    You can strip the HTML bits, it's just the PHP based bits i'd like to see..

  5. Tammy Hart
    Member
    Posted 5 years ago #

    if there is more than one loop on the page, it doesn't work, unless your condition is right after the first loop, which obviously isn't doable if the conditional is in the footer. I'm looking for a workaround for this myself...

  6. Tammy Hart
    Member
    Posted 5 years ago #

    aha!

    As explained here: http://codex.wordpress.org/The_Loop#Multiple_Loops_Example_1, you wouldn't query $wp_query twice, which if you have a normal query and are using query_posts on the same page, you are. the solution is outlined on that page, define a new query object. Fixed it right up for me!

  7. Mark / t31os
    Moderator
    Posted 5 years ago #

    Or reset the query after it's run...

    <?php wp_reset_query(); ?>

    WP seems to have a function for just about everything, if only it could cook dinner to... :)

  8. doncampbell
    Member
    Posted 5 years ago #

    @t31os_ Thanks - I was having a similar problem and putting the <?php wp_reset_query(); ?> right before my is_home() call did the trick!
    -Don

  9. tgiokdi
    Member
    Posted 5 years ago #

    aw lordy, I spent a couple hours banging my head against my desk trying to figure this out. resetting the query fixed my problems, thanks a ton guys!

  10. negs
    Member
    Posted 4 years ago #

    Awesome! This worked for me too, when I was trying to create a special sidebar for the front page. is_front_page() was not working and I couldn't figure out why.

    I put <?php wp_reset_query(); ?> as the first line in my sidebar and all is now well.

    Thanks!!!!

  11. robbono
    Member
    Posted 4 years ago #

    That simple <?php wp_reset_query(); ?> just totally rocked my world - I, too, use a highly customized frontpage with multiple loops, and I wanted to check is_home() in the header.php to selectively load image preloading scripts and other heavy javascripts.

    is_home was totally borked until I used the wp_reset_query() right in front of the conditional, and now it works perfectly! Rock on! Thanks to t31os_ and tammyhart for the quick and easy solution!

  12. tenaciousJk
    Member
    Posted 4 years ago #

    So now the question is why does that work?

    Isn't this just a bandaid for bad programming? Are there any performance drawbacks to running the reset_query?

  13. Mark / t31os
    Moderator
    Posted 4 years ago #

    Nope not a bandaid ...

    See the source here.
    http://core.trac.wordpress.org/browser/trunk/wp-includes/query.php

  14. secci
    Member
    Posted 4 years ago #

    i've been wondering why i could never get the front page and headspace to work, it was because of the multiple loops... god dam this has been bugging me for weeks.. thanks for the fix!

Topic Closed

This topic has been closed to new replies.

About this Topic