
Something that comes up on a regular basis is how to display certain content on different pages or if certain conditions are met. One way to accomplish this would be to create separate templates for each page. However, if you have a bunch of pages this can quickly become a pain. The other way to accomplish this would be to use WordPress’ is_page() function to display content when you want.
First let’s take a look at what the function is. According to the codex
“This Conditional Tag checks if Pages are being displayed. This is a boolean function, meaning it returns either TRUE or FALSE.“
Ok, that’s not the most helpful description ever. A simpler explanation would be that the tag checks the name of the page and returns TRUE or FALSE. This means you can put a nice little echo in there and have it display whatever you want.
So let’s say you have one page.php file. This is the template you use on every page on your site. On your about page you would like to add a secondary navigation. I don’t know why you want to, let’s just say you do. This navigation sits above the content area, so you can’t just add it into your page through the WordPress wysiwyg editor. Again, you could create a new template for the page, but that is boring. Instead, where you want the second navigation to be, enter this into your page.php file.
<?php if ( is_page('about')) { echo '<div id="secondnav">
This is the super cool secondary navigation.
</div>' ; } ?>
The code is asking if the page is named “about”, if it is, it echo’s the div “secondnav”. If the page isn’t called “about” then it echo’s nothing and nothing is displayed. It’s that simple. If you want the nav to display on the “about” and “contact” page add this
<?php if ( is_page('work') || is_page('contact')) { echo '<div id="secondnav">
<div class="secondnavs"><a href="http://www.creative303.com/work/">Work</a></div><div class="longsecondnavs">.</div>
</div>' ; } ?>
One place I find myself using this a lot is loading javascript only on pages that need it. One problem of adding javascript links to your footer.php file is that they are called on everypage whether you need them or not. The simple way to call them only when need is to add them like this
<?php if ( is_page('about')) { echo '<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>'; } ?>
This works well for pages, but for your blog page and single posts you will need a different function.
For your blog page use
is_home()
For your single posts use
is_single()
WordPress provides a slew of other tags as well including
is_home(), is_front_page(), is_search(), is_404(), is_singular(), is_page(), is_attachment(), is_local_attachment(), is_single(), is_sticky(), is_archive(), is_category(), is_tag(), is_author(), is_date(), is_year(), is_month(), is_day(), is_time(), is_admin(), is_preview(), is_paged(), is_page_template(), is plugin active(), is_plugin_page(), is_new_day(), is_feed(), is_trackback(), is_comments_popup(), comments_open(), pings_open(), is_taxonomy(), is_taxonomy_hierarchical(), is_term(), is_user_logged_in(), is_blog_installed(), is_active_sidebar(), is_dynamic_sidebar(), is_active_widget()
As you can see, you can target pretty specific events and conditions. So have fun experimenting with conditional tags and if you come up with something cool, let me know in the comments.
I’m just a guy interested in all things design and web related. You should contact me about about this article, for freelance work, or for any reason.
16 Comments
Great post, thanks. Although I must admit that it’s hard to focus on the post with that graphic of the WordPress embossed Moleskine notebook staring me in the face! I must have one…
Thanks, I feel the same way about the notebook. I need to find out where he got that from.
My page template contains an Adsense block which I want to display on all pages except my Privacy Policy and Contact Us pages. Can you give me an example of how I would bypass the Adsense on certain pages and display it on the remainder?
@Bill, there are a couple ways you could solve your problem. One way would be to place your Adsense div inside the php code and string together all pages you want it shown on. So you keep adding to is_page(‘work’) || is_page(‘contact’) where ‘work’ and ‘contact’ are the names of the pages where you want it to show. You can add as many as you want, just separate each by the double pipe ||. Another, possibly simpler way, would be to just add another style sheet inside the php code. Create a ‘noadsense.css’ file and in that declare the Adsense div to be hidden. You could do something like
#adsense{
display:none !important;
}
This will only call the css file on pages you don’t want the block to show. Instead of listing the page names of the pages you want the div displayed on, you list the pages you want the div to be hidden.
So it really depends on whether it is easier to exclude the pages you want it on, or the pages you don’t. I would go with whatever the shorter one is.
Hope that helps? If not feel free to ask for more clarification.
Thanks, Travis for your prompt reply. I’m just a little dense when it comes to editing the php files. I’ve done quite a bit over a period of time but not with conditional statements. In my page_template I have a line like this:
I want to display this php for all my pages except two–called “Privacy Policy” and “Contact Us.” I’ve worked with COBOL for years and use “IF THEN ELSE” statements. Is there any format like that? Thanks again.
Travis, my example line didn’t show up in my question. Here it is:
“”
Trying again:
“include(‘page_adsense.php’)” (leaving out the php commands)
Sorry for the slow reply, had to get some real work done today
K, I think I know what you’re doing. The simple way for you would be to add this to your header.php file, before the </header> tag.
<?php if ( is_page('privacy-policy') || is_page('contact-us')) { echo '<link rel="stylesheet" href="http://whereeveryouputthenewcssfile.css" type="text/css" media="screen" />' ; } ?>Then inside your new css file add this
#div id of ad block{display:none !important;
}
The block of ads should now be hidden on your privacy and contact page. One key thing to note in the above php snippet, the page name has to be the way it is displayed in your url. Thus the lowercase and dash, if you don’t use permalinks you can get the post id by going to your edit page page, and hovering over the link to edit one of your pages, at the bottom of your browser, you can see the post id #. It ends with “edit&post=#’ the # is the post id.
So there you go, best of luck and please let me know how it turns out. Or feel free to follow up with more questions.
And in other news your comments helped me realize I had smileys enabled.
So thanks for your questions.
Okay, first thanks for your patience. I did what I understood you to say but I didn’t find a “/header” tag in my header.php so I placed it in front of one that was “hr /”. Then I created a noadsensestyle.css and inserted the lines into it but nothing changed.. Now, what I understood may not be what you meant. Instead, please take a look at the website in question: http://www.prostatehealthsource.com. In my page.php file there is an include for page_adsense.php. Could I just bypass this include in the case of my two other pages?
I have no posts on my site–only pages. Thanks again.
Another way you could do it is by using a custom template for each page you don’t want it on.
You can do this by copy and pasting your page.php file into a new file. Call it something like, noads.php
Remove the include page_adsense.php part. Now go back to the top of your new php file. Here put,
< ?php/*
Template Name: No Ads
*/
?>
Add the new noads.php to your theme directory. Now edit a page in the wordpress admin area. Below the update button is a place that lets you select a Template. It probably says ‘Default Template’ drop down the menu and you should be able to select the new ‘No Ads’ template. Update the page and you should no longer have the ads block included.
Is that a little easier? Let me know how it goes.
note: is_page has a problem with secondary loops in page – use wp_reset_query() to fix. see here – http://wordpress.org/support/topic/265424
@rcain Thanks for pointing that out.
Great post! Not only answered my question, but helped clarify what I previously only half-understood about WordPress’ Boolean structure,
exactly looking for this…thanks alot
Thanks for the post. I was looking for this since long time.
11 Trackbacks/Pingbacks
Leave a comment