If you haven’t read part one, please go ahead and do that first.
Now we know how the if statement works, and how we can use it with WordPress’ conditional tags, we want to do a else statement!
Case I
I want to display a message on the front page, and another on single posts, and a third on pages. Then i want to have a standard message for the rest of the pages on the site
OK, what we looked at in part 1 was only the if statement, but if you want to do one thing for one page, and another thing, for another page, it would be easiest to use the else (and elseif) statement. Let us look at how we could accomplish this:
-
<?php if ( is_home() ) { ?>
-
<p>This is the front page!</p>
-
<?php } elseif ( is_single() ) { ?>
-
<p>This is a single post!</p>
-
<?php } elseif ( is_page() ) { ?>
-
<p>This is a page!</p>
-
<?php } else { ?>
-
<p>This could be any type of page but not the frontpage, a single post or a page</p>
-
<?php } ?>
So here we, in english, do a IF showing the frontpage THEN message ELSE IF showing a single post THEN message ELSE IF showing a page THEN message ELSE message. If you dont want anything to happen if the current page is not the frontpage, a single post or a page, then you could just exclude the last else statement - you don’t have to finish a line of elseif’s with a else.
6 comments
WordPress theme park » How to use conditional tags? Says:
[…] Links: WordPress: Conditional Tags WordPress: Conditional Tags - 2 […]
29/03-2006 | 11:44
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:43
WordPress theming guide | The undersigned Says:
[…] Before I start fixing the code up etc, I just want to hear some wishes, on what you want from a theme, and if there is any specific parts of the WordPress integration you want to get explained more detailed than others. I will make sure the theme makes use of a lot of different techniques, such as conditional tags and loopcounters. […]
21/09-2006 | 9:51
Ilya Says:
Thank you for a usefull article. But one more question - if I want to display something specific only on the first page of my blog which is www.mydomain.com
and when user goes to the another “front page” as www.mydomain.com/index.index.php?paged=2 I want to see something different from the first page - is it possible somehow?
As I guess I should put some parameter into is_home() tag or not?
24/05-2007 | 11:07
Ilya Says:
Well, sorry for “spamming” your article, but I found out a decision for my question.
Here it is http://wordpress.org/support/topic/100001?replies=9
If we would like to display something only on the first index page we should put such php code
24/05-2007 | 11:20
cenzi Says:
you rock my man… it was PERFECT and EXACTLY what I was looking for.
12/07-2007 | 18:57
Leave a Reply