Title: Conditional statement, CPT, single, with tag
Last modified: August 21, 2016

---

# Conditional statement, CPT, single, with tag

 *  Resolved [Angie Meeker](https://wordpress.org/support/users/ameeker/)
 * (@ameeker)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/)
 * When writing conditional statement that includes has_tag, do you have to include
   the custom post type if the taxonomy’s attached to it only? So… if the CPT is
   x, and has tag Y, and is single, then… OR, just is single and has tag?
 *     ```
       if ( !is_single() || has_tag( 'tours')  )
       		return;
       ```
   
 * Should that return what follows it on single pages with the tag tours? (it’s 
   not, and I tried has_term, too).

Viewing 15 replies - 1 through 15 (of 18 total)

1 [2](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/page/2/?output_format=md)

 *  [Christian Chung](https://wordpress.org/support/users/christian1012/)
 * (@christian1012)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/#post-4012986)
 * What you posted is confusing. In human words, that code says, “If the current
   post is NOT a single page OR has the tag ‘tours’, do stuff.
 * Regardless, is it a custom taxonomy?
 *  Thread Starter [Angie Meeker](https://wordpress.org/support/users/ameeker/)
 * (@ameeker)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/#post-4013048)
 * 🙂 Ok, so that’s supposed to be if it’s a single page, AND it has the tag “tours”
   which yes, is a custom taxonomy, “attractiontype”
 * The CPT is attractions
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/#post-4013054)
 * review [http://codex.wordpress.org/Conditional_Tags#A_Taxonomy_Page](http://codex.wordpress.org/Conditional_Tags#A_Taxonomy_Page)
 *  Thread Starter [Angie Meeker](https://wordpress.org/support/users/ameeker/)
 * (@ameeker)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/#post-4013058)
 * [@alchymyth](https://wordpress.org/support/users/alchymyth/) I did before I posted–
   I don’t post without trying things myself first.
 * I tried it FIRST with `if ( is_single() && has_term( 'tours') )` and that didn’t
   work, then with has_tag, and that didn’t work. Then, like `if ( is_single() &&
   has_term( 'tours', 'attractiontype') )` and that didn’t work.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/#post-4013062)
 * where in what template are you using the code?
 *  Thread Starter [Angie Meeker](https://wordpress.org/support/users/ameeker/)
 * (@ameeker)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/#post-4013065)
 * functions.php
 * Here’s the whole of it:
 *     ```
       /**
        *
        * Display an tour using
        * Genesis column classes and ACF.
        *
        * @author Angie Meeker
        * @uses   Advanced Custom Fields
        */
       add_action( 'genesis_post_content', 'theme_prefix_tour' );
       function theme_prefix_tour() {
       	// Return early if not a single page with tours tag
       	if ( !is_single() || has_tag('tours')  )
       		return;
   
       	// Store the tour data
       	$tour_data = array(
       		'contact' => get_field( 'contact' ),
       		'admission' => get_field( 'admission' ),
       		'hours'    => get_field( 'hours' ),
       		'coaches_accepted'      => get_field( 'coaches_accepted' ),
       		'reservations_required'     => get_field( 'reservations_required' ),
       		'length_of_tour'       => get_field( 'length_of_tour' ),
       		'location_of_parking'       => get_field( 'location_of_parking' ),
       		'comp_policy'       => get_field( 'comp_policy' ),
       		'restrooms_available'       => get_field( 'restrooms_available' ),
       		'dietary_menu_available'       => get_field( 'dietary_menu_available' ),
       	);
   
       	// Only output if we have tour data
       	if( $tour_data ) {
       	echo '<div class="details">Tour Information</div>';
       		echo '<div class="location-wrap one-half first">';
       			if ( $tour_data['contact'] ) {
       				echo '<div class="location">' . esc_attr( $tour_data['contact'] ) . '</div>';
       			}
       			if ( $tour_data['admission'] ) {
       				echo '<div class="location">' . esc_attr( $tour_data['admission'] ) . '</div>';
       			}
       			if ( $tour_data['hours'] ) {
       				echo '<div class="location">' . esc_attr( $tour_data['hours'] ) . '</div>';
       			}
       			if ( $tour_data['coaches_accepted'] ) {
       				echo '<div class="location">' . esc_attr( $tour_data['coaches_accepted'] ) . '</div>';
       			}
       			if ( $tour_data['reservations_required'] ) {
       				echo '<div class="location">' . esc_attr( $tour_data['reservations_required'] ) . '</div>';
       			}
       			if ( $tour_data['length_of_tour'] ) {
       				echo '<div class="location">' . esc_attr( $tour_data['length_of_tour'] ) . '</div>';
       			}
       			if ( $tour_data['location_of_parking'] ) {
       				echo '<div class="location">' . esc_attr( $tour_data['location_of_parking'] ) . '</div>';
       			}
       			if ( $tour_data['comp_policy'] ) {
       				echo '<div class="location">' . esc_attr( $tour_data['comp_policy'] ) . '</div>';
       			}
       			if ( $tour_data['restrooms_available'] ) {
       				echo '<div class="location">' . esc_attr( $tour_data['restrooms_available'] ) . '</div>';
       			}
       			if ( $tour_data['dietary_menu_available'] ) {
       				echo '<div class="location">' . esc_attr( $tour_data['dietary_menu_available'] ) . '</div>';
       			}
       		echo '</div>';
       	}
   
       }
       ```
   
 * Only with the !single does it NOT show on archive views. What I’d like is for
   all of the Tour output (from the <details>Tour Information</details> on) to NOT
   display at all unless an Attractions post is tagged Tours.
 *  Thread Starter [Angie Meeker](https://wordpress.org/support/users/ameeker/)
 * (@ameeker)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/#post-4013067)
 * The field echo just fine – they all show up in the posts… I’m just having trouble
   figuring out that conditional in such a way that they only show up when I want
   them to! 🙂
 *  Thread Starter [Angie Meeker](https://wordpress.org/support/users/ameeker/)
 * (@ameeker)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/#post-4013183)
 * I’ve also tried `is_singular` and `if ( is_singular( 'attractions' ) && has_term('
   tours', 'attractiontype' ) )` neither of which worked. The Tour Information is
   showing (first problem) on all posts and pages, not just on Atractions, and not
   just on those with the term tours.
 *  Thread Starter [Angie Meeker](https://wordpress.org/support/users/ameeker/)
 * (@ameeker)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/#post-4013184)
 * Here’s all of my functions file if that helps. I really can’t figure this out.
 * [http://pastebin.com/tQxKHxiM](http://pastebin.com/tQxKHxiM)
 *  Thread Starter [Angie Meeker](https://wordpress.org/support/users/ameeker/)
 * (@ameeker)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/#post-4013185)
 * Also, here are some links that might be helpful:
    [http://worthington.angiemeekerdesigns.com/attractions/](http://worthington.angiemeekerdesigns.com/attractions/)
   <– Attractions [http://worthington.angiemeekerdesigns.com/whattodo/tours/](http://worthington.angiemeekerdesigns.com/whattodo/tours/)
   <– Tours [http://worthington.angiemeekerdesigns.com/attractions/zip-zone-canopy-tours/](http://worthington.angiemeekerdesigns.com/attractions/zip-zone-canopy-tours/)
   <– A Tour with no tour info filled out [http://worthington.angiemeekerdesigns.com/attractions/downtown-worthington/](http://worthington.angiemeekerdesigns.com/attractions/downtown-worthington/)
   <– A tour with tour info filled out [http://worthington.angiemeekerdesigns.com/attractions/worthington-pools/](http://worthington.angiemeekerdesigns.com/attractions/worthington-pools/)
   <– An attraction with tour info filled out, but NOT tagged as tour.
 *  [Nowell VanHoesen](https://wordpress.org/support/users/nvwd/)
 * (@nvwd)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/#post-4013186)
 * This looks like what will be happening to each post/cpt in ‘the loop’.
    What 
   is the initial query that is being looped over?
 *  [alphablossom](https://wordpress.org/support/users/alphablossom/)
 * (@alphablossom)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/#post-4013189)
 * Hello,
 * I’m not 100% sure I’m following, but I think so 🙂
 * You only want to show the $tour_data if it’s the single page of the attractions
   custom post type, with a tag of “tours”?
 *     ```
       add_action( 'genesis_post_content', 'theme_prefix_tour' );
       function theme_prefix_tour() {
   
       	if ( is_singular( 'attractions' ) && has_tag('tours')  ) {
   
       		// display $tour_data here
   
       	}
   
       }
       ```
   
 *  Thread Starter [Angie Meeker](https://wordpress.org/support/users/ameeker/)
 * (@ameeker)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/#post-4013192)
 * Yeah. I have that in the functions file above (in the pastebin)
 *  [alphablossom](https://wordpress.org/support/users/alphablossom/)
 * (@alphablossom)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/#post-4013193)
 * Ah, I was looking at the one you posted above.
 * Your functions file has this:
 *     ```
       if ( is_singular( 'attractions' ) && has_term ( 'tours', 'attractiontype' ) )
                       return;
       ```
   
 * You’re saying in your functions that if it’s the single attractions page with
   tours to do nothing…
 * but is sounds like you want the opposite, so if you have your conditions inside
   your if statement it should do what you need.
 * I tested it out (for tag, not term) and it seemed to do what you want.
 *  [Jon Brown](https://wordpress.org/support/users/jb510/)
 * (@jb510)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/#post-4013194)
 * I’m still not clear WHAT conditional you are trying to write.
 * > When writing conditional statement that includes has_tag, do you have to include
   > the custom post type if the taxonomy’s attached to it only?
 * The answer is no if all you care about is if the current $post object has a tag
   then you just need
 *     ```
       if (has_tag('slug')) {
       // do stuff
       }
       ```
   
 * However it looks like your writting a conditional to return early (more efficient
   code), that is usually written as a negative statement
 *     ```
       if (!has_tag('slug'))
          return;
       // do stuff
       ```
   
 * In your code however you’ve written:
 *     ```
       has_term ( 'tours', 'attractiontype' )
       ```
   
 * which won’t work. To use multiples it needs to be an array
 *     ```
       has_term ( array('tours', 'attractiontype') )
       ```
   

Viewing 15 replies - 1 through 15 (of 18 total)

1 [2](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/page/2/?output_format=md)

The topic ‘Conditional statement, CPT, single, with tag’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 18 replies
 * 6 participants
 * Last reply from: [Angie Meeker](https://wordpress.org/support/users/ameeker/)
 * Last activity: [12 years, 10 months ago](https://wordpress.org/support/topic/conditional-statement-cpt-single-with-tag/page/2/#post-4013235)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
