• 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)
  • 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:

    https://codex.www.ads-software.com/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

    (@licinio)

    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.