Have you ever noticed how some pages on blogs behave different than on other pages? One of the most under-utilised features of WordPress is the wide variety of conditional tags available in WordPress. Through the use of conditional tags, you can instruct certain plugins, pictures, or code to only appear on designated pages.
Recently 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 WordPress.
Here is a list of the conditional tags available:
- 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()
One of my favorite places to use conditional tags is in the post meta area. For example, on most pages I want the comments button to display, but I don’t need it to appear on the single page because the comments are displayed on that page. At the same time, I like having an edit button on the single page, but I don’t need it on any other pages.
Here is the code I use for the above example on one of my sites:
<?php if (is_single()){ ?> <?php edit_post_link(__("*Edit*"), ''); ?> <?php } else { ?> | <?php comments_popup_link('0 comments', '1 comment', '% comments'); ?> <?php } ?>
The bold code is the conditional tags I’ve set up. You can see from the above code that I am telling WordPress to only display an Edit button on single pages, and on all other pages display the comments link.
What other good uses have you found for conditional tags?



Wednesday, February 20th, 2008 at 5:48 am
I write posts in both my native language (danish) and english, and I use the “is_category()” tag to show a small danish flag in front of the headlines for the danish posts.
Wednesday, February 20th, 2008 at 9:50 am
I have write a Template to include in a Theme and you can easy lern the Conditional Tags or debug a Theme, see
http://bueltge.de/wordpress-theme-debuggen/536/
It give it a little bid more Tags:
* is_admin Wenn im Adminbereich
* is_archive Ob ein Archiv dargestellt wird
* is_attachment Anhang im Beitrag oder Seite
* is_author Autorenseite
* is_category Kategorie Archiv oder Parameter
* is_tag Tag-seite
* is_comments_popup Kommentar per Popup Funktion
* is_date Archivseite auf Basis Datum
* is_day Archivseite auf Basis Tag
* is_feed wenn es ein Feed ist
* is_home Startseite (nicht nur die statische Seite)
* is_month Archivseite auf Basis Monat
* is_page statische Seite
* is_paged Seite/ Weblog wurde „geblättert”
* is_preview Vorschau
* is_robots
* is_search Suchseite
* is_single einzelner Beitrag
* is_singular
* is_time Archivseite auf Basis Zeit
* is_trackback wenn es es sich um Pingback oder Trackback handelt
* is_year Archivseite auf Basis Jahr
* is_404 Fehlerseite 404
see: http://wordpress-buch.bueltge.de/conditional-tags-erkennen/29/
With best regards
Frank
Wednesday, February 20th, 2008 at 11:18 am
Okay, since I’m green, where exactly do you insert that code? Could you provide a screenshot? I guess I should ask, would you please provide a screenshot? You mentioned it was in the post meta … (that is where?) Thanks. You’ve got a great blog, and I enjoy learning from it.
Wednesday, February 20th, 2008 at 3:21 pm
@ M-alo – That is a GREAT idea!
@ Frank – Thanks for providing that information!
@ PublicRecordsGuy – That comment about the post meta section was just an example I was providing. The information such as author name, categories/tags, post date, etc.
With that said, once you learn how to use them, you can use functions pretty much anywhere you want to!
Thursday, February 21st, 2008 at 1:45 am
Welcome!
Trackbacks/Pingbacks
Leave A Comment
Become one of our
Featured Sites
Recent Trackbacks
Contributing Authors
Archives
Extras
WordPress Hacks Copyright © 2007-2009 | An Apricot Media Website
Template by StudioPress | Custom Design by Kyle Eslick and Blog Design Studio
RSS Feed Email RSS