Genius is nothing but great effort applied. – Awa Kenzo

How To: Use Conditional Tags In Thesis

So you want to use WordPress Conditional Tags (or statements) in your Thesis customization. No sweat! Here is a basic example of how to make stuff show up ONLY where you want it; using Thesis OpenHook

conditional-tags-thesis

EXAMPLE CODE

<?php if (is_front_page()) { ?>
this is the homepage

<?php } else if (is_single()) { ?>
this is a post

<?php } else { ?>
this is a page
<?php } ?>

What does all that mean?

1. If I’m on the homepage, write “this is the homepage”

2. Or if I’m on a post, write “this is a post”

3. Unless I’m on a page then write, “this is a page”

It doesn’t get any simpler.

You can post the above code into any hook in Thesis OpenHook (just remember to tick “Execute PHP”)

wordpress-conditional-tag-1

and it will show

wordpress-conditional-tag-2

Let’s say you wanted Featured Content Gallery to show up on the homepage and pages only. The syntax would look like this:

<?php if (is_front_page()) { ?>
<?php global $post; include(WP_CONTENT_DIR . '/plugins/featured-content-gallery/gallery.php'); ?>

<?php } else if (is_single()) { ?>

<?php } else { ?>
<?php global $post; include(WP_CONTENT_DIR . '/plugins/featured-content-gallery/gallery.php'); ?>
<?php } ?>

Notice the blank space below is_single())  That says, “If this is a post, do nothing”.

For further reading (and kick-ass examples) see the Official list of WordPress Conditional Tags

Greg Rickaby runs on the Genesis Framework

Genesis Framework

The theme you're viewing is the eleven40 Child Theme, which was built on Genesis.

Genesis empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go. It's that simple - start using Genesis now!

Take advantage of the 6 default layout options, comprehensive SEO settings, rock-solid security, flexible theme options, cool custom widgets, custom design hooks, and a huge selection of child themes ("skins") that make your site look the way you want it to. With automatic theme updates and world-class support included, Genesis is the smart choice for your WordPress website or blog.

Comments

  1. Thanks Greg! This was driving me crazy. Works perfectly!

  2. Christine says:

    Can you please tell me how I would exclude code on certain pages, ie the “legal” pages? I’m not real good yet on writing the code, so could you please give me the code that I would use? I only want the openhook code to show on all single posts and pages with the exception of About, Contact, Privacy Policy and Disclosure pages.

    I have to fix this ASAP!

    Thank you.

  3. Amy says:

    Thanks man! I was really struggling with this, and it saved me a lot of time!

  4. Ryan says:

    After googling around, i always come back here for the RIGHT answer, thanks you are champion.

  5. Can't Do It says:

    Doesn’t work; even copied exact code and it parse errors every time. Thanks anyway.

  6. GG says:

    Hey Greg,

    This actually got me going on Thesis today…I am using Openhook and just started with the theme today and this was a huge help in getting conditionals to work in Openhook, which, as you pointed out, uses a different syntax then having to use custom_functions.php/

    Thanks for getting me out of the gate with Thesis and Openhook.

    G

  7. Dude you are just great…….Hats oFF…..Thanks to google also for sending me to the right place . searching for this since long and got the solution from you now !!
    Now my Dynamic Content gallery is showing up on the main page only :-)

  8. Question, what if I wanted to basically just have a text appear on the home page only..using Thesis Hooks? Is that possible? For example, say I wanted to say Welcome on the top of the home page, but not on other pages. Can I use Thesis Hooks to accomplish this? Thank you for this useful info by the way.

  9. Shaw Girl says:

    Hello! Your tutorials have been a HUGE help for me thus far! I do, however, have one problem. I was able to get the Featured Content Gallery slider on my blog using the Thesis Open Hook code from your other post. However, the slider was showing up on all of my pages, when I only want it to show up on my home page. When I implemented your code above, it replaced ALL of my pages with the home page (i.e. my “About” page, “Blog Roll” page, “Recipes” page and “What’s In Season” page all were replaced with the front page). I definitely didn’t want this but do want the slider to stay only on my home page. Any suggestions? I’ve left my site with the slider on each page since I didn’t want all my pages to look like my home page. Thanks so much!

  10. Shiela says:

    Hello ,

    I am testing my Thesis copy in local host and trying to setup it up
    i want to show Featured Content Gallery in home page and blog page but i am running into problems.
    In someone told me to add this code to thesis

    Use this conditional :

    if (is_front_page() || is_home())

    But not sure where to put it

    Please advise

    Thank you

  11. Trisha says:

    Would you happen to know how to write this to change a css class on a specific page? Say, if you’re on the page called blog, switch div id=”header” to div id=”header-blog”? I need to change the header image on that page only. Any help would be greatly appreciated!

    • You would use conditional tags in custom_functions.php to call a new stylesheet. For example here is what I do:

      
      // Build Conditional CSS
      function single_back() {
      if (is_page('966')){
      ?>
      <link rel="stylesheet" href="<?php bloginfo('template_url');?>/custom/single.css" type="text/css" media="screen, projection" />
      <?php } }
      add_action('wp_head', 'single_back');
      
  12. Few people use the is_front_page() – should be used more!

  13. Greg, articles like this make me glad I follow you on Twitter! Thanks!

  14. Which rather neatly answers my question on another article about placing the Featured Content Gallery on the home page — and teaches me how to handle similar stuff in the future. Thanks!

Trackbacks

  1. [...] How to Use WordPress Conditional Tags in Thesis – Conditional tags allow you to specify that certain elements of your design only appear on certain posts/pages of your blog. This tutorial will show you how its done. [...]

  2. [...] by Greg Rickaby on December 30, 2009 I’m sure this tutorial has been done a hundred-times over, but I blog so I don’t have to remember. This function will help you eliminate a bloated, self-serving plug-in like Yoast Breadcrap; by using the ever popular (and super useful) conditional tags… [...]

  3. [...] How To: Use WordPress Conditional Tags In Thesis (tags: thesis conditional thesiswp wordpress tags custom functions php openhook howto tutorial) [...]

Speak Your Mind

*