Support

Account

Home Forums General Issues Hide Links That Have No Content

Solved

Hide Links That Have No Content

  • Hi – great plugin!

    I was able to add the following code to my page to setup download link for uploaded files: <p><a href="' . get_field('upload_document') . '" target="_blank" class="readmore">Download PDF → </a></p>

    How would I modify this as to not display on my page if there is no file to download?

    Also, is there a way to display the TITLE given to the file when uploading, as opposed the the Download PDF → in my code?

    Thanks,
    John

  • if get_field('upload_document'){ . '<p><a href="' . get_field('upload_document') . '" target="_blank" class="readmore">Download PDF → </a></p>' . };

    Yes it is possible to use the title from the uploaded document, but you’ll need to set the field to return the File ID instead of the File URL. With the ID you can retrieve the values like this…

    <?php
    $attachment_id = get_field('upload_document');
    $url = wp_get_attachment_url( $attachment_id );
    $title = get_the_title( $attachment_id );
    if( get_field('field_name') ):?>
      <a href="<?php echo $url; ?>" >Download File "<?php echo $title; ?>"</a>
    <?php endif; ?>
  • Thanks! I found a similar post to this last week but it takes down my entire site.

    I feel that I am missing something on my end. Here are all the custom field code I’m using:

    <p><?php the_field('vendor'); ?></p>
    			<p><?php the_field('manufacturer'); ?></p>
    			<p><?php the_field('component'); ?></p>
    			<p><?php the_field('component_description'); ?></p>
    			<p><?php the_field('model_or_part_number'); ?></p>
    			<p><?php the_field('part_classification'); ?></p>
    			<p><?php the_field('information'); ?></p>
    			<p><?php the_field('upload_document'); ?></p>
    			<p><?php the_field('upload_document_(2)'); ?></p>
    			<p><?php the_field('upload_document_(3)'); ?></p>
    			<p><?php the_field('upload_document_(4)'); ?></p>

    I thought I could modify it using your recommendation:

    <p><?php the_field('vendor'); ?></p>
    <p><?php the_field('manufacturer'); ?></p>
    <p><?php the_field('component'); ?></p>
    <p><?php the_field('component_description'); ?></p>
    <p><?php the_field('model_or_part_number'); ?></p>
    <p><?php the_field('part_classification'); ?></p>
    <p><?php the_field('information'); ?></p>
    if( get_field('upload_document') ): ?><a href="<?php echo $url; ?>" >Download File "<?php echo $title; ?>"</a><?php
    endif;
    if( get_field('upload_document_(1)') ): ?><a href="<?php echo $url; ?>" >Download File "<?php echo $title; ?>"</a><?php
    endif;
    if( get_field('upload_document_(2)') ): ?><a href="<?php echo $url; ?>" >Download File "<?php echo $title; ?>"</a><?php
    endif;
    if( get_field('upload_document_(3)') ): ?><a href="<?php echo $url; ?>" >Download File "<?php echo $title; ?>"</a><?php
    endif;

    but this kills my site…

    I know I’m missing something, but I can’t see it…

    Thanks!

  • If this is the exact code you are using you’re missing your opening <?php on line 8 and the closing ?> on the last line. You also need to set the $title and $url variables if you haven’t already.

    <p><?php the_field('vendor'); ?></p>
    <p><?php the_field('manufacturer'); ?></p>
    <p><?php the_field('component'); ?></p>
    <p><?php the_field('component_description'); ?></p>
    <p><?php the_field('model_or_part_number'); ?></p>
    <p><?php the_field('part_classification'); ?></p>
    <p><?php the_field('information'); ?></p>
    
    <?php
    $attachment_id = get_field('upload_document'); // Get and set the ID
    $url = wp_get_attachment_url( $attachment_id ); // Get document url from ID
    $title = get_the_title( $attachment_id ); // Get Title from ID
    if( get_field('upload_document') ): //If the document field is populated do... ?>
      <a href="<?php echo $url; ?>" >Download File "<?php echo $title; ?>"</a>
    <?php endif; ?>
    
    <?php
    $attachment_id = get_field('upload_document_2'); // Get and set the ID
    $url = wp_get_attachment_url( $attachment_id ); // Get document url from ID
    $title = get_the_title( $attachment_id ); // Get Title from ID
    if( get_field('upload_document_2') ): //If the document field is populated do... ?>
      <a href="<?php echo $url; ?>" >Download File "<?php echo $title; ?>"</a>
    <?php endif; ?>
    
    <?php
    $attachment_id = get_field('upload_document_3'); // Get and set the ID
    $url = wp_get_attachment_url( $attachment_id ); // Get document url from ID
    $title = get_the_title( $attachment_id ); // Get Title from ID
    if( get_field('upload_document_3') ): //If the document field is populated do... ?>
      <a href="<?php echo $url; ?>" >Download File "<?php echo $title; ?>"</a>
    <?php endif; ?>
    
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Hide Links That Have No Content’ is closed to new replies.