• 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 Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    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 ) ) {
    
    Thread Starter dpipitone

    (@dpipitone)

    Thanks so much!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome

    Thread Starter 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 Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    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.

    Thread Starter dpipitone

    (@dpipitone)

    Got it. Thank you for all your help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Conditional Value When No Taxonomy Terms Assigned’ is closed to new replies.