is_home() no longer works!
Leave a response | Trackback |
Tags: conditional tags, WordPress
You hacked your WordPress homepage because you don’t intend to make a blog out of it and then all of a sudden the very useful conditional tag <?php if(is_home()) ?> no longer works. It happened to me twice and for that 2 occurrences, I couldn’t find a solution until a couple of days ago.
I’m working on this project where I don’t need to show my posts anywhere aside from the category archive and single, not even in the homepage. What the client wants is that the home page should show all of the categories in the site, no matter if it’s empty or not and a certain page content must be pulled into it. Because I’m lazy in creating a page template just for that and assigning that page as the home page, I modified the main index template instead.
Those things are actually easy to do. All I have to do is to write this within the main index template:
<?php wp_list_categories('order_by=name&hide_empty=0&title_li='); ?>
so it’ll show all of the categories ordered by the category name whether it has posts or not. And for the page to be pulled into the main index, query_post is the answer:
<?php query_posts('page_id=ID'); while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); endwhile; ?>
so it’ll show the title and the contents of the page ID.
It worked just as expected and I was so proud of myself until the client asked that the sidebar for the home page be different from the rest of the site. That’s when I knew that the is_home() doesn’t work.
I searched high and low for in the codex and support forum but couldn’t find anything. I can’t tell the client that I can’t fix this so I tried my hunch if it works. I’m not the type who is a big fan of plugins because I’m afraid that if the site rely heavily on plugins and and if and when that certain plugins’s developer goes AWOL, the site might go berserk due to incompatibility and all I could do was search again for a plugin that could replace it.
I thought that the home page is being treated by wordpress as if it’s a page because I did pull the contents of a page into it. Turns out that I was right!
When I put <?php if(is_page('ID')) { ?> in the sidebar, it worked just as I wanted it to.
So now, I realized that once you’ve changed the content of your main index template, know that you are also taking out it being your “home” because it will start acting like a page (if you pulled a page into it).


Wait, really? is_home still works for me… I just tried it a few minutes ago.. sa WP 2.3.3 din. bakit kaya? hmmm.
it doesn’t on certain occassions waaaa di mo binasa yung buong article
hehe
yeah, it happened also to me when i was creating a template.