I current have a custom field that is displaying even if there is not data inputted in the meta box when posting.

enter image description here

How do I remove the hr, title, and ul if there is not data input?
Here’s the code I have currently to pull in the custom field:

<?php if( have_rows('google_drive_links') ): ?>
<hr />
     <h3>Attachments</h3>
     <ul class="google-drive-links">
<?php while( have_rows('google_drive_links') ): the_row(); 
     // vars
     $content = get_sub_field('google_link_name');
     $link = get_sub_field('google_link'); ?>
     <li class="google-drive-link-item">
     <a target="_blank" href="https://wordpress.stackexchange.com/questions/309194/<?php echo $link; ?>"><?php echo $content; ?></a>
    </li>
<?php endwhile; ?>
</ul>
<?php endif; ?>

1 Answer
1

It could be that there are rows being returned for:

have_rows('google_drive_links')

but then, nothing returned for the sub fields?

$content = get_sub_field('google_link_name');
$link = get_sub_field('google_link'); 

Perhaps you could put in an extra check for those two before creating the hr and ul?

if (get_sub_field('google_link_name') &&  get_sub_field('google_link')){
   // Create the hr and ul 
}

Depending on where you want the hr and h3 to appear, possibly like this:

<?php if( have_rows('google_drive_links') ): ?>
    <?php while( have_rows('google_drive_links') ): the_row();
         // vars
         $content = get_sub_field('google_link_name');
         $link = get_sub_field('google_link');
         if ($content && $link) : ?>
             <hr />
             <h3>Attachments</h3>
             <ul class="google-drive-links">
                 <li class="google-drive-link-item">
                     <a target="_blank" href="https://wordpress.stackexchange.com/questions/309194/<?php echo $link; ?>"><?php echo $content; ?></a>
                 </li>
             </ul>
         <?php endif; ?>
     <?php endwhile; ?>
<?php endif; ?>

If there are multiple rows of links, it should be possible to add a counter to make sure the hr and title is only added once, for example:

<?php
$counter = 0;
if( have_rows('google_drive_links') ): ?>
    <?php while( have_rows('google_drive_links') ): the_row();
         // vars
         $content = get_sub_field('google_link_name');
         $link = get_sub_field('google_link');
         if ($content && $link) :
             $counter ++;
             // If there is content and link, create hr and title for first item only, open ul and create li
             if ($counter == 1) : ?>
                 <hr />
                 <h3>Attachments</h3>
                 <ul class="google-drive-links">
             <?php endif; ?>
             <li class="google-drive-link-item">
                 <a target="_blank" href="https://wordpress.stackexchange.com/questions/309194/<?php echo $link; ?>"><?php echo $content; ?></a>
             </li>
         <?php endif; ?>
      <?php
      endwhile;
      if ($counter > 0) : ?>
          <!-- Close ul -->
          </ul>
      <?php endif; ?>
  <?php endif; ?>

Leave a Reply

Your email address will not be published. Required fields are marked *