Support » Plugin: Custom Post Type UI » Conditional Value When No Taxonomy Terms Assigned

  • Resolved dpipitone

    (@dpipitone)



    I have set up a custom Taxonomy for “Now Pouring” and I would like to know how to display a message “Not available right now.” when there aren’t any values assigned.

    Right now, my page’s template is using:

    <?php if ( get_the_term_list( $post->ID, 'portfolio_category', '', '<br />', '' ) ) : ?>
    							<div class="project-info-box">
    								<h4><?php esc_html_e( 'Now Pouring At:', 'fusion-core' ) ?></h4>
    								<div class="project-terms">
    									<?php echo get_the_term_list( $post->ID, 'pouring_at', '', '<br />', '' ) ;?>
    								
     
    								</div>
    							</div>
    
    						
    						<?php endif; ?>
    

    Can anyone help?

    Thanks in advance

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    Should be as simple as providing an else portion of your if statement, like so:

    <?php 
    if ( get_the_term_list( $post->ID, 'portfolio_category', '', '<br />', '' ) ) : ?>
    	<div class="project-info-box">
    		<h4><?php esc_html_e( 'Now Pouring At:', 'fusion-core' ) ?></h4>
    		<div class="project-terms">
    			<?php echo get_the_term_list( $post->ID, 'pouring_at', '', '<br />', '' ) ;?>
    		</div>
    	</div>
    <?php
    else :
    ?>
    Do something.
    
    <?php endif; ?>
    

    Also, I would recommend using something like https://codex.wordpress.org/Function_Reference/has_term for your if statement check. You can check for ANY term in the taxonomy by passing an empty string for the first argument.

    if ( has_term( '', portfolio_category', $post->ID ) ) {
    
    dpipitone

    (@dpipitone)

    Thanks so much!

    Plugin Author Michael Beckwith

    (@tw2113)

    Welcome

    dpipitone

    (@dpipitone)

    Hey, Michael.

    I tried this out, but when I remove the taxonomy values, it doesn’t display the message:

    <?php 
    if ( get_the_term_list( $post->ID, 'portfolio_category', '', '<br />', '' ) ) : ?>
    	<div class="project-info-box">
    		<h4><?php esc_html_e( 'Now Pouring At:', 'fusion-core' ) ?></h4>
    		<div class="project-terms">
    			<?php echo get_the_term_list( $post->ID, 'pouring_at', '', '<br />', '' ) ;?>
    		</div>
    	</div>
    <?php
    else :
    ?>
    echo "Not available";
    
    <?php endif; ?>
    Plugin Author Michael Beckwith

    (@tw2113)

    Part of why I recommended changing to has_term for your if statement. Technically the way you’re doing things now is likely producing *some* sort of output, which is resulting in the first half evaluating to true. Essentially, it could be testing for a true condition with just “<br/”, aka if ( '<br />' ) { which will evaluate to true cause it has a value of some sort. “Truthy” value if I recall technical terms correctly. The has_term function will return only boolean true and false values making it more consistent and reliable.

    dpipitone

    (@dpipitone)

    Got it. Thank you for all your help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.