Support

Account

Home Forums General Issues Display/hide multiple fields based on value

Solving

Display/hide multiple fields based on value

  • I’m using ACF to display testimonials on portfolio items on my website. These testimonials contain the following data:

    – The testimonial text
    – The name of the person who wrote the testimonial
    – An image of the person who wrote the testimonial

    There’s no problem displaying all these items when all data is available but some testimonials don’t have an image available and sometimes there’s no testimonial available at all.

    I’m currently using the following code to display the testimonial that only shows the image when there’s one available.

    <blockquote><p><?php the_field('text_testimonial'); ?></p>
    						
    <footer>
    
    <?php 
     
    $image = get_field('photo_testimonial');
     
    if( !empty($image) ): ?>
    <img class="testimonial-portfolio" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
     
    <?php endif; ?>
    
    <p><?php the_field('name_testimonial'); ?></p></footer>
    						
    </blockquote>

    But how can I display or hide the entire blockquote/testimonail when there’s no testimonial text available? And when there’s an text available, how make another check for the image?

  • Hi,

    you should store your other field in a variable as well,

    Then before rendering your blockquote your run an if statement that check if the testimonial is available, if it’s not it skips the blockquote part

    someting like:

    
    $testimonial = get_field('testimonial');
    
    if($testimonial && !empty($testimonial)){
    
    // build your quote blockquote with the if img... 
    
    }

    this case doesn’t test for the name if you want the testimonial AND the name you should write:

    
    $testimonial = get_field('testimonial');
    $name = get_field('name);
    
    if($testimonial && !empty($testimonial) && $name && !empty($name)){
    
    // build your quote block with the if img... 
    
    }

    I haven’t tested this code but an if statement before your <blokquote> building is what you need, you can find more info about the if “or”, “and” conditions here if you want to set up something more complex.

    http://www.php.net/manual/en/language.operators.logical.php

  • Thanks for your reply Pirondini! It looks good but just copy/pasting the blockquote code in between the brackets broke it.

    I’ll see if I can debug it myself as the PHP code will be tweaked a bit to make it work. My PHP skills ain’t the best 😉

  • let me know if you still having issues

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

The topic ‘Display/hide multiple fields based on value’ is closed to new replies.