Home > Net >  Hide ACF fields in the front-end when one of them is empty
Hide ACF fields in the front-end when one of them is empty

Time:02-22

I am currently trying to show fields in the front-end, however they're files/uploads. So they're not always used. I am trying to work out how you hide fields when there is no upload. I currently have this code after researching some bits, but it isn't working. I do apologise, I am currently still learning PHP.

Any help would be greatly appreciated.

function woo_new_tab_content() {


   // The new tab content
   if (get_field ('tech_sheet'));
   echo '<h3><a href="'. get_field('tech_sheet') .'" target="_blank">Download Tech Sheet</a></h3>';
   if (get_field ('datasheet'));
   echo '<h3><a href="'. get_field('datasheet') .'" target="_blank">Download Datasheet</a></h3>';
   if (get_field ('datasheet_2'));
       echo '<h3><a href="'. get_field('datasheet_2') .'" target="_blank">Download Datasheet</a></h3>';
       if (get_field ('datasheet_3'));
   echo '<h3><a href="'. get_field('datasheet_3') .'" target="_blank">Download Datasheet</a></h3>';


       }

CodePudding user response:

Please try this:

    function woo_new_tab_content() {
   // The new tab content
   if(get_field ('tech_sheet'))
   {
    $file1 = get_field ('tech_sheet');
    echo '<h3><a href="'. $file1["url"] .'" target="_blank">Download Tech Sheet</a></h3>';
   }
   
   if (get_field ('datasheet'))
   {
    $file2 = get_field ('datasheet');
    echo '<h3><a href="'. $file2["url"] .'" target="_blank">Download Datasheet</a></h3>';
   }
   
   if (get_field ('datasheet_2'))
   {
    $file3 = get_field ('datasheet_2');
    echo '<h3><a href="'. $file3["url"] .'" target="_blank">Download Datasheet</a></h3>';
   }

   if (get_field ('datasheet_3'))
   {
    $file4 = get_field ('datasheet_3');
    echo '<h3><a href="'. $file4["url"] .'" target="_blank">Download Datasheet</a></h3>';
   }
       }

CodePudding user response:

Thank you.

That works, but the URL doesn't work. It just creates a URL of the current URL, not the loaded file. Rather than

  echo '<h3><a href="'. $file4["url"] .'" target="_blank">Download Datasheet</a></h3>';

could we not do this? or would it not work?

 echo '<h3><a href="'. get_field('datasheet') .'" target="_blank">Download Datasheet</a></h3>';
  • Related