The Lodge is members-only design/dev videos and Office Hours.

Next Office Hours Session: "Implementing an SVG Icon System" Nov 30 - 6:00 PM Eastern

Call get template part if condition is true?

  • # September 26, 2012 at 8:44 pm

    Defeat by php again.

    I have created a gallery that loops through some custom post types and a category. I am using < ?php get_template_part ?> to call the gallery. However, the title is outside of the loop and I am only calling the gallery if there are any post within a certain category. But my title still shows up due to it being outside of the loop. Calling it inside the loop is obviously not an option. How can I work around this so the title will only show up if the category is true?

    Gallery Title

      < ?php
      my loop

    # September 26, 2012 at 9:35 pm

    @fectio1 post your code on jsfiddle so I can see it properly. Sounds like an if-statement is needed here.

    # September 27, 2012 at 9:59 am

    @ChristopherBurton I tried a few if statements, but was unsuccessful in calling it. My goal is to only get the

    title to show up if the category is true. Hopefully that makes sense.

    # September 27, 2012 at 10:45 am

    I figured it out. I had to use term_exist. Here is an example of my code.

    $term = term_exists(‘your_category_name’, ‘category’);
    if ($term !== 0 && $term !== null) {

    # September 27, 2012 at 11:05 am

    @fectio1 What does it output?

    # September 27, 2012 at 11:06 am

    @fectio1 Oops. Just noticed you had solved it.

    # September 27, 2012 at 11:00 pm

    This reply has been reported for inappropriate content.

    just as a follow-up, you need to use html entities when writing html tags:


    will result in:




    will result in:


Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed