• Resolved flofluse

    (@flofluse)


    Hola gente,
    this is the first time i write here, up to now i always found a answer scanning throug the docs and th eforum, but not this time.

    i need to get the url of a uploaded file, which isn’ t embedded in the post content.
    How can i do that?

    i tried the function wp_get_attachment_url(52); but it doesn’t work. it doesn’t print out anything.
    I called the function inside a loop:

    <?php query_posts('cat=10'); //retrieves content from category 10/'materials' only ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
       <h4><?php the_title(); ?></h4>
       <?php the_content(__('(more...)')); ?>
    
       <?php echo wp_get_attachment_url(52); ?>
    
    <?php endwhile; else: ?>
    <p><?php _e('Lo siento, no encuentro el contenido...'); ?></p>
    <?php endif; ?>

    i read somewhere that attachments have to be embedded in the post content to be recognized. But i can’t do this because i want to print out the post content somewhere else.
    Anyway the function doesn’t work for me with the embedded upload neither.

    to explain a little more what i try to achieve:

    My idea is to make a download page with grafic buttons, which are links and contain a title and a description text.
    There are 3 diffrent types of buttons (text, grafic, video).

    So i created a category ‘downloads’ and every post of this category has additionally the tag ‘text’, ‘grafic’ or ‘video’.
    From the post title and the post content i get the title and description of the file for the button (therefore i don’t want to embedd the file in the content). with the tag i can control which button image i use.

    But i need the url of the file to make the link work…

    what can i do?

    thanks for your help
    flo

Viewing 2 replies - 1 through 2 (of 2 total)
  • You’d need to query the database for any attachments to the post in question, as wp_get_attachment_url() would work only on the attachment itself (which is a special kind of post), and not the post it is *attached* to.

    Anyway, this will collect and display all attachment urls for the current post:

    <?php
    $attachments = $wpdb->get_col("SELECT ID FROM $wpdb->posts where post_parent= $post->ID and post_type = 'attachment'");
    foreach( $attachments as $attachment ) :
    ?>
    <?php echo wp_get_attachment_url($attachment); ?><br />
    <?php endforeach; ?>

    If there will only be one attachment for each post, here’s a simplified version:

    <?php
    $attachment = $wpdb->get_var("SELECT ID FROM $wpdb->posts where post_parent= $post->ID and post_type = 'attachment'");
    ?>
    <?php echo wp_get_attachment_url($attachment); ?>

    Thread Starter flofluse

    (@flofluse)

    Hey! thank you very much, kafkaesqui. It works now . Juppii!!

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

The topic ‘How to get the URL of a uploaded file’ is closed to new replies.