Using Conditional Statements In WordPress

October 22nd, 2009

addicottweblogoAddicott Web has a great little article online that goes in depth on the uses of WordPress conditional statements. The Conditional Tags can be used in your Template files to change what content is displayed and how that content is displayed on a particular page depending on what conditions that page matches. These statements enable a wide range of flexibility when it comes to when and where to display content. In fact, I use conditional tags all the time with my use of the Widget Logic plugin. I tell the widget which page I want it to be displayed on with a conditional statement and it only shows up on that page.

I love these things. Let me know in the comments what you’ve been able to achieve using them in your own theme or widgets.




  1. Adam W. Warner (1 comments.) says:

    My WP development hands would be tied without Conditional Statements + Widget Logic.

  2. Jessi Hance (4 comments.) says:

    I use them in my theme header to set the page title, depending on what kind of page it is. A lot of people do that, I guess.

    I also use them to create meta descriptions. On single post pages, the post excerpt is used for the meta description. On “page” pages, it uses a custom field where I put in whatever description I want for that page; and on the home page, I just echo out the meta description I want.

    These bits of code enabled me to stop using a couple of SEO plugins. I have a lot of plugins and I’m always glad to find a way to prune them.

    Oh, I also use them to do menu highlighting, and to control the content of the sidebar based on which page it is.

  3. Matt (23 comments.) says:

    Conditional statements are truly amazing. I use them for:

    1. Displaying a different layout graphic for home view and single/archive/page/search/other views.

    2. Styling a post differently for the “mature audiences only” category so that there is a verification link to show the content instead of just outright displaying it (both in archive/category/search view and single view).

    3. Sidebar shenanigans for home/single/page.

    I’m sure I’ll come up with some more ways to use them and trim the fat out of the theme code in the future.

  4. Jake Spurlock (1 comments.) says:

    I use conditional tags to show/hide content based on if the user is logged in or not.

    Ex. If the user isn’t logged in, when they get to an event registration page, they are told to register or login, if they are, then they can see the page and register.

    I have also done the same thing with ads, show create account buttons, but turn them off once they have logged in. Wrote up a blog post about it too…

  5. One Fine Jay (1 comments.) says:

    I use it to nuke comments from pages.

  6. jason (1 comments.) says:

    category pages – each category has its own icon next to the category name (it’s a sports blog, so each category has the logo of the appropriate team). conditional statement lets me display different logos via a different css class on different category pages without creating a zillion new templates.

  7. Tony (4 comments.) says:

    This sounds like something I have been looking for, thankls for bringing it to my attention.

    I have been trying to find a way to display a different advert block in the sidebar, depending on the country that a visitor is from.

    I know the conversion from IP Address to country is not bulletproof, but if I could for example display UK related ads to visitors from the UK and USA related ads to visitors from the USA (and use this as a default), it could make a difference to my potential revenue.

    Initially I have thought to use this for adverts, but there are many other uses, like displaying variable RS Feeds.


  1. […] Using Conditional Statements In WordPress […]

Obviously Powered by WordPress. © 2003-2013

page counter