Support

Account

Home Forums General Issues How to display a Video or Image Field

Solving

How to display a Video or Image Field

  • Sorry if this has already been answered but could not find an answer. I am new to ACF and PHP and I have been trying to figure this out on my own. No success.
    I have created 2; fields one to upload a VIDEO and one to upload an IMAGE. Both work in my template on their own. I need to create the option of either/or so that if there is no VIDEO and an IMAGE has been uploaded then the image will be displayed. Here is my code so far:

    <?php     
      $value = get_field('event_video');
             if( $value ) {
                       
            echo $value;
    
           } elseif $value == 'null'; {
    
           echo $image = get_field('event_image');
    
            if( !empty($image) ): ?>
                    <img src="<?php echo $image['url']; ?>" alt="<?php echo    $image['alt']; ?>" /> 
                        <?php endif;
                              
               }  
             ?>

    Thanks,
    Shaun.

  • This reply has been marked as private.
  • Hi @shaundvine

    Please change your code to this:

    <?php     
      $value = get_field('event_video');
             if( $value ) {
                       
            echo $value;
    
           } else {
    
           $image = get_field('event_image');
    
            if( !empty($image) ): ?>
                    <img src="<?php echo $image['url']; ?>" alt="<?php echo    $image['alt']; ?>" /> 
                        <?php endif;
                              
               }  
             ?>
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘How to display a Video or Image Field’ is closed to new replies.