Support

Account

Home Forums General Issues I can't display any file from a file field

Solved

I can't display any file from a file field

  • Hi,
    I created a file field, it’s working, but my problem is I can’t display the file when I use this simple code :

    <?php
    if( get_field('myfield') ):
        ?><a href="<?php the_field('myfield'); ?>" >Download File</a><?php
    endif;
    ?>

    And if I try this code :

    <?php 
    $value = get_field('myfield');
    echo '<pre>';
    print_r( $value );
    echo '</pre>';
    ?>

    I get everything good :
    Array
    (
    [id] => 140
    [alt] =>
    [title] => Finish
    [caption] =>
    [description] =>
    [mime_type] => image/jpeg
    [url] => http://localhost:8888/mysite/wp-content/uploads/2015/02/31.jpg
    )

    I also tried that :

    <?php
    $attachment_id = get_field('myfield');
    $url = wp_get_attachment_url( $attachment_id );
    $title = get_the_title( $attachment_id );
    
    if( get_field('myfield') ):
        ?><a href="<?php echo $url; ?>" >Download File "<?php echo $title; ?>"</a><?php
    endif;
    ?>

    I don’t understand because if I try to display $url, I have nothing…

    So I really don’t understand why the url is not good when I try to display the file.

    Thank you

  • Ok, I find my mistake, I forget to set one parameter in the file field :

    return value : File URL

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

The topic ‘I can't display any file from a file field’ is closed to new replies.