post-page

is_home(), is_archive() not Working? Here is a Fix

13
responses
by
 
on
January 16th, 2010
in
WordPress Tips

Quite recently, is_home() and is_archive() stopped working for me. I mean, that these conditional tags stopped working for me and the intended code I had was not performing as expected.

I did a bit of digging around, however I did not find any conclusive answers to what was wrong. However, I did find a solution at this forum post, and after including an additional snippet it started working fine.

If you are facing a problem with the is_home() function, you can use the wp_reset_query(); function before you use the conditional checks to fix the issue.

I am still looking into the internal code as to why this is happening, will update the post once I find an answer to this problem.

heading
heading
13
Responses

 

Comments

  1. I will bookmark this post for future reference..thanks

  2. For me its still working nice on WordPress 2.9.1… 😕

  3. mercime says:

    Learned wp_reset_query from MichaelH at WordPress.org forums – especially for conditional statements in sidebar.
    Cheers.

  4. westi says:

    The problem is likely that you are trying to use the template tags after you have run a second query and loop in your page and so you have overwritten the default query that WordPress runs for your page/post.

    Basically, you have called query_posts() to fetch some extra information and now the global $wp_query object references your new query (as that is what you want so all the template tags work when you run it as a loop).

    Once you have finished with this second query you need to call wp_reset_query() so as to set the $wp_query back to be the one that WordPress ran for the current post/page.

    Then these template tags will reflect that query.

    • Note that it’s often a plugin that’s responsible. Some plugins really screw with the global wordpress variables. You’ll commonly see the effect with widgets, where installing a new, poorly-written widget plugin causes others to break.

    • Ashfame says:

      That explains everything! 🙂

  5. I wrote an article about this problem. pls check this link http://blog.manchumahara.com/2.....ing-in-wp/

  6. bubazoo says:

    I’ve always had that problem with those condition tags, I don’t know waht your talking about. lol

  7. I have noticed many plugin developer used query_posts which is problematic for conditional tags, Sometimes conditional tags works fine in header.php but give wrong result in footer.php and other section. So my own work or any plugin I install I always search is there any string matching query_posts and use my own technique to solve

    in functions.php (in my current theme) I put a function

    function php4_clone($object) {
    if (version_compare(phpversion(), '5.0') < 0) {
    return $object;
    } else {
    return @clone($object);
    }
    }
    //end php4_clone

    WordPress has a function named wp_clone() which can be used too.


    and use this to keep backup of current default wp query and restore after custom query using query_posts

    //keep backup
    $temp_query = php4_clone($wp_query);
    ...
    query_posts(....);
    ....
    //restore
    $wp_query = php4_clone($temp_query);

    Many times I got bad situation for this as conditional tags were not working in many places.

    guys let me know how u think about my code style.

  8. James says:

    Hi

    I have the folowing code in my sidebar.php file for WordPress

    ` post_content;?>
    post_content;?>
    post_content;?>
    post_content;?> `

    But the conditional archive code isn’t working as you can see here:

    http://www.thebreadcrumbtrail.org/archives/533

    I have noted however, that my archive file is called archive.php without the ‘s’.
    But, I changed it to s, and it made no difference.

    Any ideas warmly welcomed!

  9. serge says:

    i’m executing the same code that works fine in wp 3.05 and breaks in wp 3.1

    brief
    my own plugin ovewrites wp_query after wp_reset_query to get posts by their type (executed in the middle of wp_head hook )

    then a custom code from functions.php checks the query at the very end of the wp_head hook (99) and it shows that the query args where changed to get posts by their type (custom type “painting” ) but it shows wp_query is_archive 1 and empty taxonomy or term …

    weird,
    looks like is_home but shows like is_archive

    any ideas ?



Trackbacks/Pingbacks

  1. […] is_home(), is_archive() not Working? Here is a Fix […]

Obviously Powered by WordPress. © 2003-2013

css.php