Support » Fixing WordPress » is_home() is not working in my sidebar, doesn’t even return false

  • Resolved staiano

    (@staiano)


    I want to dynamically change some of what shows in my sidebar whether I am on the homepage, one of my 3 main categories or all all other pages. I tried to use is_home() like the following but it doesn’t work.

    if (is_home()) { /* home */

    } elseif ( in_category(3) ) { /* a post from cat 3 */

    } elseif ( in_category(4) ) { /* a post from cat 4 */

    } elseif ( in_category(5) ) { /* a post from cat 5 */

    } else { /* generic pages like about us */

    }

    I am not doing anything special in index.php other than to limit the number of post returned with query_posts(“showposts=1”); so I am not sure why it is not working.

    I tried used is_front_page() instead of is_home() but that didn’t help me at all either. I even tried to echo what is_home() and is_front_page() and I don’t even get false returned [no value is return].

    I know I could have a special siderbar for the home page with say get_sidebar(‘home’); but since only part of the sidebar is different that doesn’t seem like the right solution.

    Help?

    Thanks,
    staiano

Viewing 8 replies - 1 through 8 (of 8 total)
  • So if you do this:

    echo 'this if value for is_home' . is_home();
    if (is_home()) { /* home */

    Does it print something?

    Im having the same problem, are you using 2.7?

    I had the same problem and I could not for the life of me figure out why it happened. Just prior to it happening I made an adjustment to the number of posts show on the front page. I noticed that the option for setting a static front page was missing. This led me to find a post that stated if you do not have any pages on your site the option in settings -> reading for setting a static front page will be hidden. The problem I think occurred (at least for me) when I made a change to the reading settings page while those options were hidden. They somehow messed up the systems check for front page. I created a page and then went back to settings->reading and selected posts and saved. Now is_front_page and is_home are working great again.

    i hope this helps you as well.

    Calling the below function before checking for is_home() may solve your problem. I had this issue and got solved in this way. This function resets the previous query.

    <?php wp_reset_query(); ?>

    Sometimes even calling
    <?php wp_reset_query(); ?>
    may result in some side effects. Once it is called, further queries such as in_category() results failure due to reset. For more details refer
    http://wordpress.org/support/topic/160374?replies=4

    To circumvent all these issues my universe solution is to introduce global php varaibles such as $my_home, #my_category to replace is_home(), in_category() respectively.

    In the index.php, single.php, archives.php I set these variables appropriately just before calling sidebar_x().

    In all sidebar widget just check for status of (either 1 or 0) $my_home, $my_category… in widget don’t forget to declare these variables as global.

    looks dirty work around, but it works like charm in any condition, any theme.. 🙂

    Yep, you’re not alone. Just spent an hour banging my head against the wall… I was sane enough to paste the code into a different template file to verify that it was supposed to work. wp_reset_query() worked for me.

    Perhaps if you try the approach suggested here.

    http://codex.wordpress.org/Function_Reference/get_sidebar#Mullti_sidebars

    Instead of doing your IF/ELSE in the sidebar, do it in your template file and use alternate sidebars..

    You can see the function here..
    http://core.trac.wordpress.org/browser/trunk/wp-includes/general-template.php

    Of course there’s nothing to stop you writing your own get_sidebar_custom() function that takes additional parameters…

    @Swamtyk, thank you for this tip, it worked for me!

    I have content that I want to show/hide in the footer and sidebar based on what page I’m on. Unfortunately, WordPress doesn’t allow you to use is_home() multiple times…

    So I setup a few global variables in the top of the index.php file, which will act as messengers to show/hide elements in the sidebar.php and footer.php files.

    At the top of my index file I have:
    <?php global $footer_display; ?>

    If I had multiple variables to declare, I would separate the variables with commas:
    <?php global $footer_display, $sidebar_display; ?>

    Then, when I query if the page is_home, I set the value to 1:
    <?php if (is_home) { ?>
    //blah blah
    <?php $footer_content = 1 ?>

    This variable is tested in the footer for a 1 value, and displays content accordingly…. so in essence, I’m able to turn things off and on throughout the site using the if statements I run in the index.

    HOWEVER, you must re-instantiate the global variable (not the value, just the variable) at the top of each separate file! So I also put this at the top of my footer file:
    <?php global $footer_display; ?>

    See here:

    global $variables in header not passed to body

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘is_home() is not working in my sidebar, doesn’t even return false’ is closed to new replies.