Title: Help making a conditional download button
Last modified: August 19, 2016

---

# Help making a conditional download button

 *  [licinio](https://wordpress.org/support/users/licinio/)
 * (@licinio)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/help-making-a-conditional-download-button/)
 * Hi!
 * I’m using Custom field template and I have a custom field for a file, problem
   is there may not always be a file to download….
 * So…
 * Can Anyone help me make the download button a condition?
 * Something like…
 * if there’s a file show download link else show nothing
 * My problem is translating this to php… :S
 * Thanks in advance for any help!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [David Gwyer](https://wordpress.org/support/users/dgwyer/)
 * (@dgwyer)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/help-making-a-conditional-download-button/#post-1678190)
 * I have done this lots of times and below is a code snippet that will download
   all the values for a custom field key. Remember that you can have multiple duplicate
   entries for the same named custom field key. This is tremendously useful as it
   is gathered into a php array for processing using the:
 * `get_post_custom_values('enter_field_key', $post->ID);`
 * function. You can see more info about it here:
 * [http://codex.wordpress.org/Function_Reference/get_post_custom_values](http://codex.wordpress.org/Function_Reference/get_post_custom_values)
 * There are other choices to manipulate the custom field data, this is just one
   example that I have used over and over again. The advantage to you is that you
   can use it to handle any number of file downloads per post, you are not just 
   limited to one. 🙂
 * Anyway, onto the code:
 *     ```
       $file_download_array = get_post_custom_values('enter_field_key', $post->ID);
       if(count($file_download_array) >= 1) {
           if(count($file_download_array) > 1) { // multiple download files to show
               foreach($file_download_array as $file) {
                   // add code here to handle/show files
               }
           } else { // single download file to show
               // add code here to handle/show file
           }
       }
       ```
   
 * Just change the ‘enter_field_key’ with your custom field key, and you are good
   to go!
 *  Thread Starter [licinio](https://wordpress.org/support/users/licinio/)
 * (@licinio)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/help-making-a-conditional-download-button/#post-1678523)
 * Hi!
 * Thanks for the reply!
 * I can’t get it to work…
 * Also it seems to me that this code looks to see if there are multiple files and
   list them…
 * It’s great and much appreciated, but I need a code that besides that shows nothing
   if there are no files…
 * Once again, thank you very much for helping! 🙂

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Help making a conditional download button’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [licinio](https://wordpress.org/support/users/licinio/)
 * Last activity: [15 years, 8 months ago](https://wordpress.org/support/topic/help-making-a-conditional-download-button/#post-1678523)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
