• tanjilahsan

    (@tanjilahsan)


    Hi,
    I want to get first or any attachment(audio/video) file link/url to use it as a direct download link anywhere in my WordPress site.
    I used several wp functions such as
    ‘wp_get_attachment_url( $id )
    get_attachment_link()
    wp_get_attachment_link()
    get_attached_media( $type, $post_id )’
    and other similar functions. But they got no result for me. Most of the time I got an empty array.
    I’m using a modified twentytwelve wp theme. I didn’t modify WordPress code, just the theme codes are modified.
    Other general PHP functions are working fine. Is there anything I missed or out of my knowledge? Can anyone help me out please?
    Thank you

Viewing 8 replies - 1 through 8 (of 8 total)
  • esmi

    (@esmi)

    If this is outside the Loop, then you will need to pass a post id to most of those functions.

    Thread Starter tanjilahsan

    (@tanjilahsan)

    Dear esmi,
    Every time I tried from inside the loop and also tried with parameters.

    Thread Starter tanjilahsan

    (@tanjilahsan)

    One thing I found on WP codex…
    The $id of wp_get_attachment_url( $id ) which is attachment id. But don’t know how to get an attachment id. Somewhere else I found that, this $id is post id. Then I tried with $post_id. But got nothing…..:(

    esmi

    (@esmi)

    Can you show some examples?

    Thread Starter tanjilahsan

    (@tanjilahsan)

    Here is an example of my code:

    <?php $posts = get_posts("category=14&numberposts=5&offset=0"); ?>
    if( $posts ) : ?>
    foreach( $posts as $post ) : setup_postdata( $post);?>
    <div class="music01_details leftmargin">
    <h2><?php the_title(); ?><h2>
    <?php
    $post_id=get_the_ID();
    $file_link=wp_get_attachment_url($post_id);
    ?>
    <a href="<?php echo $file_link;?>">Download</a>
    <?php endforeach;
    endif;?>

    esmi

    (@esmi)

    Try:

    <?php $posts = get_posts("category=14&numberposts=5&offset=0"); ?>
    if( $posts ) :
    foreach( $posts as $post ) : setup_postdata( $post);?>
    <div class="music01_details leftmargin">
    <h2><?php the_title(); ?><h2>
    <?php
    $file_link=wp_get_attachment_url($post->ID);
    ?>
    <a href="<?php echo $file_link;?>">Download</a>
    <?php endforeach;
    endif;?>
    Thread Starter tanjilahsan

    (@tanjilahsan)

    opps! I got a typing mistake on the last of the 2nd line, which is an extra ?> please ignore that.
    Thank you

    Thread Starter tanjilahsan

    (@tanjilahsan)

    Thank you for your afford.
    But still I got nothing. I printed the $file_link variable and got empty!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Can't get audio/video attachment link/url inserted into wordpress post’ is closed to new replies.