
After my last post involving including custom content, I’ve received questions about doing the reverse, excluding content on certain pages.
As with everything there are several ways to accomplish this. I’ll cover some of the easiest.
The first way is the same as including custom content. The only difference is you have to list every page besides the ones you don’t want it to show on. This can be quite consuming if you have a large number of pages. On a small portfolio site though, this is totally useful.
<?php if ( is_page('work') || is_page('contact')) { echo '<div id="secondnav">
<div class="secondnavs"><a href="http://www.travisberry.com/video/">Video</a></div><div class="longsecondnavs">.</div>
</div>' ; } ?>
If you had three pages; ‘work’, ‘contact’, and ‘blog’, the above code would display the div ‘secondnav’ on every page but ‘blog’.
Another way is to use the above method but target it to the page you want excluded. So,
<?php if ( is_page('blog')) { echo '<link rel="stylesheet" href="http://whereeveryouputthenewcssfile.css" type="text/css" media="screen" />' ; } ?>
Can be used to exclude a section of content. Let’s say you still don’t want to show the ‘secondnav’ div on the blog page. So create a new .css file. Name it whatever you like and put it where ever you like. Just replace the url in the example with a link to the new file.
Inside the new .css file put this;
#secondnav{
display:none !important;
}
The ‘secondnav’ div will no be invisible on the ‘blog’ page.
One final, less conditional way of doing this would be to create a new template for the page. Create a file called, ‘nonav.php’.
Copy and paste everything from your normal ‘page.php’ file and paste it into ‘nonav.php’. Find the part you want removed and delete it from the file. Move to the very top of the ‘nonav.php’ file. Paste this into the file.
<?php
/*
Template Name: NoNav
*/
?>
Save the file and upload it to your current theme directory.
Now in the WordPress admin area, edit the page you don’t want the ‘secondnav’ div in. On the right side of the screen, there is a place that allows you to pick a template. Select the drop down menu and select your new template. Hit the update post button, and your page should no longer have the ‘secondnav’ div.
There you go, a few way’s to exclude content from certain pages. The best way to decide between excluding or including content is to go with whichever is the shortest. If you have 100 pages and 1 doesn’t need something, exclude. If the 1 needs something, and the 100 don’t, include.
If you have other solutions, or any questions, let me know in the comments.
Edit:
kylegetsspam over on reddit pointed out another simple way of doing the same thing.
Add,
<?php body_class(); ?>
To your body tag. Then add this to your main style sheet.
#page-id-2 #secondnav { display: none; }
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.
3 Comments
Thanks for sharing, I usually use the template method and I should really exploit the is_page() function more – great post
I’ve got a bit of code that I want on all pages, but not on a few. The site is over 100 pages. Is there a way to use is_page to exclude? Like is_page(-27)?
JK ,
use the following code:
if (!is_page(’628′) )
3 Trackbacks/Pingbacks
Leave a comment