want to display the download link whenever there is a file to download

my Problem is, like the title say, that i want to display the Download Link to a file only when a file available is…

i dont know where the error is:

Read More
<?php $doc = get_post_meta(get_the_ID(), 'wp_custom_attachment', true); ?>

<div id="custom_pdf">  

 <a href="<?php echo $doc['url']; ?> "> 

 Download PDF Here  

 </a> 

 </div><!-- #custom_pdf --> 

This is the normal code.. its work fine, but here it displayed unconditionally… and the Code for conditionally is:

<?php $doc = get_post_meta(get_the_ID(), 'wp_custom_attachment', true); ?>

<? if(strlen(trim(<?php $doc['url'] ?>)) > 0) { 
<div id="custom_pdf">  

 <a href="<?php echo $doc['url']; ?> "> 

 Download PDF Here  

 </a> 

 </div><!-- #custom_pdf --> 


} ; ?> // end if  

and here is somewhere the error, but i dont know where.

Can someone please help me. Thanks.

Related posts

Leave a Reply

3 comments

  1. Your PHP tags are not correctly placed in your HTML code:

    <?php $doc = get_post_meta(get_the_ID(), 'wp_custom_attachment', true);
    if(strlen(trim($doc['url'])) > 0) {
    ?>
    <div id="custom_pdf">  
    <a href="<?php echo $doc['url']; ?>">Download PDF Here</a> 
    </div><!-- #custom_pdf --> 
    <?php } // end if  
    ?>
    

    When you switch from HTML to PHP, you need to open a PHP tag <?php, and when you switch from PHP to HTML you need to close the PHP tag ?>.

  2. You are opening a <?php tag when you are already in php

    <?php 
      $doc = get_post_meta(get_the_ID(), 'wp_custom_attachment', true);
      if(strlen(trim($doc['url'])) > 0) 
      { 
    ?>
    
      <div id="custom_pdf">  
         <a href="<?php echo $doc['url']; ?> ">Download PDF</a> 
      </div><!-- #custom_pdf --> 
    
    <?php 
      } 
    ?>