• Resolved Angie Meeker

    (@ameeker)


    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)
  • 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

    (@ameeker)

    πŸ™‚ 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

    Thread Starter Angie Meeker

    (@ameeker)

    @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.

    where in what template are you using the code?

    Thread Starter Angie Meeker

    (@ameeker)

    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

    (@ameeker)

    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

    (@ameeker)

    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

    (@ameeker)

    Here’s all of my functions file if that helps. I really can’t figure this out.

    http://pastebin.com/tQxKHxiM

    Thread Starter Angie Meeker

    (@ameeker)

    Also, here are some links that might be helpful:
    http://worthington.angiemeekerdesigns.com/attractions/ <– Attractions
    http://worthington.angiemeekerdesigns.com/whattodo/tours/ <– 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/ <– A tour with tour info filled out
    http://worthington.angiemeekerdesigns.com/attractions/worthington-pools/ <– An attraction with tour info filled out, but NOT tagged as tour.

    This looks like what will be happening to each post/cpt in ‘the loop’.
    What is the initial query that is being looped over?

    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

    (@ameeker)

    Yeah. I have that in the functions file above (in the pastebin)

    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.

    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)
  • The topic ‘Conditional statement, CPT, single, with tag’ is closed to new replies.