Many people seem to have issues with their sidebar, title and I find that it could easily be solved by using conditional tags. I have written three questions, just to make sure everybody gets the idea of how the function works:
- I dont want to display the blogroll in the sidebar on the links page
- I want to write a message in the sidebar, but it should only be visible on the front page!
- I want to write a message in the sidebar, but it should be hidden on the front page and on the category pages
Let us start off with listing the conditional tags:
- is_home()
- is_single()
- is_page()
- is_category()
- is_author()
- is_date()
- is_year()
- is_month()
- is_day()
- is_time()
- is_archive()
- is_search()
- is_paged()
- is_404()
All these conditional tags can be specified even more by adding ID, name or slug into the parentheses, like if we want to do something specific for the post with id 102, we would use is_single(’102′).
The »questions« - 1
I dont want to display the blogroll in the sidebar on the links page
1 thing you have to know before you can continue - what is the ID for the links page. (In this article we say its page id 10).
The blogroll is called in the templatefile sidebar.php, so this is where we will work with a conditional tag. Look at this code:
-
<?php if ( is_page(‘10′) ) {
-
get_links_list();
-
} ?>
As you see, we do a IF is page with the id 10 is displayed THEN do this. Between the if’s start and end tag ( { } ) we specify what we want to happen.
The »questions« - 2
I want to write a message in the sidebar, but it should only be visible on the front page!
This is almost the same as the previous question, and will be solved in the exact same way. Look at this code:
-
<?php if ( is_home() ) { ?>
-
<p>This is a custom message and will only appear on the front page!</p>
-
<?php } ?>
What was different? Nothing but the conditional tag!
The »questions« - 3
I want to write a message in the sidebar, but it should be hidden on the front page and on the category pages
This is almost the same question as the above, but for this request we want to do a if is not and a or. Look at this code:
-
<?php if (!(is_home() || is_category())) { ?>
-
<p>This is a custom message and will appear on all sites, but the front page and the category pages.</p>
-
<?php } ?>
The two only differences from the earlier examples is the ! and the ||. The ! specify the phrase if is not and the ||, or. So the whole call would sound like this: IF IS NOT the front page OR a category page THEN
The end
So how about all the other conditional tags? - you can use all the conditional tags like this, and make your content much more dynamic. Using this, your sidebar won’t have to show the exact same things on all pages, and you can easily make a headerimage different on the types of pages on your blog. Read more about the conditional tags on the WordPress codex.
37 comments
Marino Says:
The answer for the third question should be
if ( !is_home() && !is_category() )
Checking with OR will make the sentence always true. If it ’s the category page, then it isnt the home page, so the first part of the conditional it’s true, making true the whole sentence.
To use ||, you can change it to (using logic properties):
if (!(is_home() || is_category()))
NOT ( A OR B) = (NOT A) AND (NOT B)
13/03-2006 | 14:23
The undersigned Says:
Oh, yea my bad :)
13/03-2006 | 14:55
Ira Krakow Says:
Very useful, especially for people new to Wordpress or not familiar with PHP. Thanks!
13/03-2006 | 15:13
Stupid Wordpress Tricks » Blog Archive » Conditional Tags in Wordpress Says:
[…] This conditional tags tutorial at theundersigned.net will show you how to do it, without getting your hands too dirty. […]
13/03-2006 | 15:14
HandySolo Says:
Good stuff. I suspect this will help answer a lot of questions over at wordpress.org/support. :-)
13/03-2006 | 15:45
miklb Says:
I think adding an …else in there would be the next logical step in a tutorial. That is, if it’s page A do this, else page B do this. I think that’s the concept that really trips up users.
13/03-2006 | 17:55
The undersigned Says:
Yea, ill make a part 2, right away :)
13/03-2006 | 18:00
The undersigned Says:
[…] If you haven’t read part one, please go ahead and do that first. […]
13/03-2006 | 18:12
Arun Says:
This is really useful. Thanks a lot :-)
13/03-2006 | 18:24
Adrienne Says:
Also, if you could add an echo example. I’m trying to have an image show up on 1 single page with a specific slug and it’s not working. So, if you can think about adding that to part 2 it would be much appreciated.
22/03-2006 | 21:10
The undersigned Says:
Actually i have done part 2: http://theundersigned.net/2006/03/wordpress-conditional-tags-2/
But if you want to show a image i suppose you want to do it like this:
[?php if ( is_single(’slug’) ) { ?]
[img src=”" alt=”" /]
[?php } ?]
(used [’s instead of <’s)
22/03-2006 | 21:25
Live WordPress theme list » How to use conditional tags? Says:
[…] Links: WordPress: Conditional Tags WordPress: Conditional Tags - 2 […]
28/03-2006 | 10:29
The undersigned » Blog Archive » From XHTML/CSS to WordPress Says:
[…] Now, back to the top navigation. We added the code from our original html file to the header.php file, but we want those buttons to be autogenerated. I have taken the snippet and manually added a home tab, and added a template tag that lists all pages. The class »current_page_item« is added by the template tag automatically, to the active tab, but not on the extra tab we create, the home tab. This is why we want to use a conditional tag (read this and ), to add the class, if the current page showed, is the home/front page. […]
03/05-2006 | 19:42
Landing sites 1.1 | The undersigned Says:
[…] ls_getinfo(’isref’) Returning true if the referrer is a supported search engine - used as a conditional tag […]
05/06-2006 | 14:59
Denise Says:
THANK YOU SO MUCH!!!!!!!!!!!!!!!!!!!!!!!!!!!! YOU HAVE EXACTLY WHAT I WAS LOOKING FOR AND IT WORKED PERFECTLY. MAN IF I HAD A MILLION DOLLARS I’D GIVE IT TO YOU. I SPENT FOREVER TRYING TO FIGURE OUT HOW TO ACCOMPLISH QUESTION NUMBER 2. THANKS AGAIN MAN. YOU’RE AWESOME!
17/06-2006 | 2:20
Karl Says:
Hi,
thanks for the info. But when I use if_category, it will only work for the category overview page, not for the single posts in the category.
I want to highlight the category of the post in the menu for every post. How can I do this?
Thanks, Karl
27/06-2006 | 12:35
The undersigned Says:
Then you can use in_category :) if(in_category(’5′) || is_category(’5′)) { }
27/06-2006 | 12:45
Sveta Says:
You guys do a wonderful job! Keep up the good work!!!t
22/01-2007 | 23:24
Kisakookoo Says:
Hi! Why I can’t fill my info in profile? Can somebody help me?
My login is Kisakookoo!
24/01-2007 | 5:59
Lu Says:
So beautiful! Now, I can stop using plugins and several diferent sodebars and single files to modify my categories and posts! Code IS poetry! :)
13/02-2007 | 21:04
Landing sites | Business Process Inc Says:
[…] ls_getinfo(’isref’)Returning true if the referrer is a supported search engine - used as a conditional tag […]
16/02-2007 | 0:11
Marc Says:
Does this work after a Loop? I’m actually using this in the footer, and it appears to work perfectly well with pages. However, anytime the conditionals are called after a Loop has been performed, it fails to work
I thought this might have something to do with the page needs to be rewound to the beginning. So, I tried rewind_posts, but that didn’t work either.
Anyone else run into the same issue.
22/04-2007 | 9:30
Marcos Says:
Thanks! This explanation was really useful to avoid posting the date in certain pages.
15/05-2007 | 16:26
Jabi Says:
My problem is different but I guess it’s related to this issue: I would like to show a specific picture on each page. You’ll say, “well, add that picture on the text editor”. But I would like to have it displayed on the upper side of the sidebar, like here:
http://www.gabrielaresti.com/web/euskaltegia/metodoa/
I’ve added the picture to the sidebar by adding a sidebar module which simply contains the html code of that fixed image.
My question is: is there a way to make it display a specific image on the sidebar related to the specific page we want it to appear on? May be using some page id related code?
19/05-2007 | 13:35
wordpress theme design tutorial | Loose Your Yeast Infection Today! Says:
[…] Now, back to the top navigation. We added the code from our original html file to the header.php file, but we want those buttons to be autogenerated. I have taken the snippet and manually added a home tab, and added a template tag that lists all pages. The class »current_page_item« is added by the template tag automatically, to the active tab, but not on the extra tab we create, the home tab. This is why we want to use a conditional tag (read this and ), to add the class, if the current page showed, is the home/front page. […]
09/06-2007 | 1:36
Notebook and Laptop Reviews Says:
good stuff , wordpress is the best
26/07-2007 | 23:41
Tom Nichols Web & Graphic Design & Something Blog » Blog Archive » links for 2007-08-02 Says:
[…] WordPress: Conditional tags | The undersigned A good explanation of how to customise conditional statements in WordPress. (tags: wordpress) […]
02/08-2007 | 13:23
Barbara Says:
This was very helpful, thank you. You don’t find anything about “if is not” in the WP Codex, but finally here I found that it is just a simple “if (!(…))”
28/08-2007 | 9:00
Michael Says:
Hello first thx for the nice hints. I wonder if it is possible exclude also Widgets from Pages this way?
For example i wanted the Weathericon http://www.viper007bond.com/wordpress-plugins/weathericon/ widget only on one page.
Michael
21/09-2007 | 13:14
jj Says:
hello …i was trying to get my header image to show up not just on the category homepage but every post in the particular category
i tried this but it makes my page totally not work …just blank screen ….i already have the is_category like you do …but once i added the in_category it stopped working…… is in_category a wordpress conditional?
Then you can use in_category :) if(in_category(’5′) || is_category(’5′)) { }
thank you so much for having this page!
02/10-2007 | 2:33
Amos » Blog Archive » Landing sites 1.3 Says:
[…] ls_getinfo(’isref’) Returning true if the referrer is a supported search engine - used as a conditional tag […]
26/12-2007 | 8:18
llbbl Says:
[…] not on the extra tab we create, the home tab. This is why we want to use a conditional tag (read this and ), to add the class, if the current page showed, is the home/front […]
24/01-2008 | 19:13
Reagan Says:
Hi can you post an example how to call a certain template like sidebar.php by using conditional tags.
Is this correct?
<?php
if (is_single(’41′) {
}
else {
}
?>
12/02-2008 | 14:21
WordPress Hack: Using WordPress Conditional Tags Says:
[…] I ran across a post by the Undersigned explaining Conditional Tags in WordPress, which appears was written in 2006 but is still valid with current versions of […]
20/02-2008 | 12:02
Interactivechris - Portfolio of Chris Rotsteeg » Portfolio adjustments Says:
[…] Header and sidebar.php adjustments : here Conditional tags: here […]
20/04-2008 | 2:21
WordPress Conditional Tags - Posted In WordPress | Bayu Albelly - .SlackbloG Journal Says:
[…] WordPress Conditional Tags […]
15/05-2008 | 9:27
WordPress Hack: Using WordPress Conditional Tags | [Blog Tutorials] Says:
[…] I ran across a post by the Undersigned explaining Conditional Tags in WordPress, which appears was written in 2006 but is still valid with current versions of […]
08/06-2008 | 3:10
Leave a Reply