Not all themes are the same though its fair to say that the majority of wordpress blogs can be divided into 4 main areas. The header, the sidebar, the footer and the main content area (eg the post, index or page areas). The main content area is the part which is different on every page of the blog. The information in the header, sidebar(s) and footer remains the same on every page of the blog. This is true for the majority of wordpress blogs.
It doesn’t have to be this way though. By using conditional statements in wordpress you can make the header, sidebar(s) and footer display different information on all areas of the site. This gives you a tremendous amount of control over the content that is displayed on your site. For example, you could display certain links on the homepage but completely different links on the rest of your blog. You could even extend the if statements to post something only home a certain post on your site. It’s very straight forward and can be very useful.
Ok, let’s look at the code!
The basic code for the php if statement is incredibly easy. Here is an example which shows some text if the page is the home page of the blog. It will not be displayed on any other page on the blog. is_home() is the function which refers to the home page.
<?php if ( is_home() ) { ?>
This would only be displayed on the home page.
<?php } ?>
Adding a simple else statement allows you to display information on the other pages of your blog. We can also add some comments to make sure the code is easy to understand at a later date.
<?php /* If page is the home page */
if ( is_home() ) { ?>
This would only be displayed on the home page.
<?php } /* for all other pages */
else { ?>
Display this on all other pages
<?php } ?>
We can extend this even further. Adding the OR operator to the if statement allows us to display content on more than one section of the blog. is_page() is the function which refers to the pages on your blog. Here is the code we can use to display our code on the home page and our wordpress pages.
<?php /* If page is the home page or a wordpress page */
if ( is_home() || is_page() ) { ?>
This would only be displayed on the home page.
<?php } /* for all other pages */
else { ?>
Display this on all other pages
<?php } ?>
Here is a list of the conditionals you can use on your blog.
The category, single and page conditionals work in the same way ie. you can pass through an id, title or page slug. Let’s look at some examples.
Say i want to display a logo in the ‘Site News’ category. The name of this category is ‘Site News, the page slug is ’site-news’ and the category id is 1.
Therefore we would use our usual if statement and add the logo in 3 different ways :
is_category(’1′) = using the category id
is_category(’Site News’) = using the category title
is_category(’site-news’) = using the category page slug
is_single(), is_author and is_page() work in the exact same way as the above.
Obviously, displaying extra unique information on every page of your site is just silly and would require hundreds of if statements however if there is some section of your blog you reall need to display something unique these conditionals will suit you down to the tee.
Practically speaking, very few people will need to pass parameters into the conditionals but displaying different info on the sidebar for the home page, pages or blog posts can be incredibly useful to you and your blog.
You shouldn’t have too many problems with this but if your unsure about anything please leave a comment or start a thread in the forums and i’ll do my best to help
Kevin
Author comments are in a darker gray color for you to easily identify the posts author in the comments
Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums
Awesome! that is exactly what I was looking for! I’m working on a new blog, and needed just that! Thanx a lot for the infos! Just made my day!
glad it helped you out Jonathan. Drop me an email if theres anything else wordpress related you want covered
Hello, great website! I just read your post on installing wordpress and it helped me out a lot to install my first blog – Hello World!
Regarding layouts – I want to custom design a 3 column layout. I can “cheat” with dreamweaver and design exactly what I want with design panel, and I know a little html to tweak it exactly to specs in the code panel.
How does this work in Wordpress? I want to place targeted ads and sponsor ads in certain sections, I dont want the header running all the way across the top, etc. Something like shoemoney’s site – http://www.shoemoney.com.
Thanks!
Also, let me add that the blog I’m working on is entertainment related and I am looking for a look like this: http://www.complex.com/blogs/
I believe they created that with Wordpress, thanks!
Hmm…this is the 5th time I am coming back to this post because I need conditional tags (i’ve bookmarked it). One of these days I should learn PHP myself.
PS: Those codes you have don’t work directly, coz there’s a space between < and ?(I assume its intentional). Why not change < to < and use it there? That way, the code won’t be messed up.