Support

Account

Home Forums Front-end Issues Show ACF only if they exists in a div

Helping

Show ACF only if they exists in a div

  • Hi, I’m new with programming and I need support to show ACF field only in some articles templates with a layout like this : https://lucagrippo.it/vini/malbec-2019-trapiche-vineyards/
    I want to stamp a div with $name (only if the field is not empty) and $value (only if the field is not empty).
    I try this code found in documentation and works well, but I need different images for each field..and I don’t want to stamp $name if is empty. Hope anyone can help me thanks

    <?php 
    
    $fields = get_fields();
    
    if( $fields ): ?>
        <div>
            <?php foreach( $fields as $name => $value ): ?>
                <h3><?php echo $name; ?></h3>
                <img src="/icone/divisore.png" />
                
                <p><?php echo $value; ?></p>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
  • 
    <?php 
      $fields = get_fields();
      if ($fields) {
        foreach ($fields as $field => $value) {
          if ($value) {
            ?>
              <div>
                <h3><?php echo $name; ?></h3>
                <?php 
                  switch ($name) {
                    case 'case':
                      $src = 'url for this field name';
                      break;
                    case 'for':
                      $src = 'url for this field name';
                      break;
                    case 'every':
                      $src = 'url for this field name';
                      break;
                    case 'possible':
                      $src = 'url for this field name';
                      break;
                    case 'field':
                      $src = 'url for this field name';
                      break;
                    case 'name':
                      $src = 'url for this field name';
                      break;
                    default:
                      // default for all field names not listed above
                      $src = 'default url';
                  } // end switch
                ?>
                <img src="<?php echo $src; ?>" />
                <p><?php echo $value; ?></p>
              </div>
            <?php 
          } // end if $value
        } // end foreach field
      } // end if fields
    ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.