I understand the basic concept of using conditional tags to display different content based on what page is being viewed. But I’m having some problems getting it to work the way I want it to. So, I have a “home.php” page, a “header.php”, a “footer.php” and a “sidebar.php”. So take this example. The “home.php” page is loading the “header.php,” “footer.php” and “sidebar.php.”
‘<?php if(is_home()) echo “<p’>You are on the home page now</p>”; ?>’
When I include this tag in the body of my “home.php” page it works fine. When I include it in my “header.php” it works fine. But when I include it in my “sidebar.php” or “footer.php” it doesn’t work. What would be different about it being included in the “sidebar.php” and “footer.php” that would cause this? Basically, I want to be able to display a different sidebar and/or footer depending on which page was being viewed. Thanks very much for any guidance or help.
Maybe because it has to load BEFORE the loop? I don’t think there’s a way around it, since header.php will be called before sidebar.php file or footer.php.
If you just want a different look for different pages, just create a different page template that has these alternate footers/sidebars in them and use those page templates for those specific pages.
Not only that, much like an Immortal, there can be only one .. sidebar.php or footer.php file to be called. Alternate files are called using the call to the template path, not get_sidebar or get_footer. Those will only call that main instance of footer.php or sidebar.php. So if you have multiple sidebars, those are called using the template path. Make sense?
Conditional tags like is_home should work anywhere. If they’re not, then maybe there is some other reason that they are not being run.
Thank you for your comments, Joni. I don’t think it’s an issue with the loop. It works fine in the header which is called before the loop. It doesn’t work in the sidebar or footer which are called after it. And yes, you’re right creating separate page templates is probably the way to go. But I’d still like to figure this issue out.
Otto42: thanks for the post. Yes, that’s what I’m trying to figure out, why is_home works fine in the header and body, but not in the footer or sidebar. If you have any ideas I’d love to hear them.
Without more information, I can’t tell you why it doesn’t work.
Allow me to demonstrate the problem I think you’re missing:
Why doesn’t my coffee maker work? Got any ideas?
I have just as much ability to answer your question as you do to answer mine. We have equal amounts of information. 😉
Understood. What kind of information might I be able to provide about this specific problem that would allow someone to offer suggestions? Or is it just too non-specific of a problem for anyone else to be able to offer any help? Thanks!
home.php, footer.php, sidebar.php to http://wordpress.pastebin.ca and post back with the URL. That might be good for a start.
Well, maybe if you posted the actual code that doesn’t work, then that might be a start. We need to see the code, what the result of that code is, what you expect it to be, etc.
For long bits of code, please post them on http://wordpress.pastebin.com and then post the links to them back here.
OK, great, thanks. I’ll get the pages in question posted.
- The topic ‘Using Conditional Tags’ is closed to new replies.