2

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="<?php echo $link; ?>"><?php echo $content; ?></a>
    </li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
  • What is the type of that custom field? Is there minimal number of rows set for that field? Because you already check if there are some values - you just don’t remove empty rows... – Krzysiek Dróżdż Jul 22 '18 at 5:53
  • I think thats the part I'm confused about. It's a repeater Advanced Custom Fields with a name and link for each row that will repeat in the post area. For some reason it's still returning something even if the meta box is left blank @KrzysiekDróżdż – Collin Jul 23 '18 at 17:16
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="<?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="<?php echo $link; ?>"><?php echo $content; ?></a>
             </li>
         <?php endif; ?>
      <?php
      endwhile;
      if ($counter > 0) : ?>
          <!-- Close ul -->
          </ul>
      <?php endif; ?>
  <?php endif; ?>
  • I think you're right - for some reason when those custom fields are left blank when posting it still must be returning something to make them display. My PHP is a little rusty - where would I add the extra check in comparison to my working code above? Thank you! – Collin Jul 23 '18 at 17:20
  • Hi @Collin, I've updated my answer including the check in your code. I haven't tested it yet though... – junkrig Jul 24 '18 at 1:17
  • That somewhat worked! So its now not showing if there is no content. The problem is that, when there is more than 1 "row" of links, they show up with the hr, header, and line as seen here. This is where I keep getting stuck :/ Thanks for the help so far though. – Collin Jul 25 '18 at 2:48
  • Hi @Collin, I've added another update. I think you could use a counter to check that the hr and title is only added once if there are multiple rows of links. I haven't tested this yet though, so it might need a bit of tweaking :-) – junkrig Jul 25 '18 at 3:12
  • It worked!!!!! Thank you :) I've been struggling on this one for a while and my other counterpart that knows PHP better couldn't figure this out either. You're the best! – Collin Jul 26 '18 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.