0

Inside the loop I would like to retrieve the URL of an inserted media file of each post.

My attempt was:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
   <a href="<?php wp_get_attachment_url(the_ID()) ?>">
     <?php the_title(); ?>
   </a>
<?php endwhile; ?>

But I can't get it to work. I made sure that there is a file inserted into each post. Furthermore I would like to ask, how it is handled if a post has more than one file.

Edit:

    <?php if ( have_posts() ) : while (have_posts()) : the_post();
    $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => null, 'post_parent' => $post->ID );
                $attachments = get_posts( $args );
                if ( $attachments ) {
                    foreach ( $attachments as $attachment ) {
                      ?>
                         <div class="col-lg-4">
                            <p class="center"> <?php  the_title(); ?></p>
                          <a href="<?php echo wp_get_attachment_url( $attachment->ID ); ?>">
                          <div id="post-<?php the_ID(); ?>" class="download-block bold">
                        DOWNLOAD
                    </div>
                </a>
            </div>
                      <?php
                    }
                }
    endwhile; endif; ?>

Edit 2:

<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
        <div class="col-lg-4 col-md-4 col-md-4 col-sm-4">
            <p class="center"> <?php echo get_the_title(); ?></p>
    <?php
                $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => null, 'post_parent' => $post->ID );
                $attachments = get_posts( $args );
                if ( $attachments ) {
                    foreach ( $attachments as $attachment ) {
                      ?>

                          <a href="<?php echo wp_get_attachment_url( $attachment->ID ); ?>">

                        DOWNLOAD
                    </div>
                </a>
            </div>
                      <?php
                    }
                }
            endwhile; endif; ?>

When I run the the_title(); outside the if($attachments) condition, it works fine, and title gives me all the titles of each post.

7
  • Where have you added this code? In which PHP file. And post full code of that file here instead.
    – Robert hue
    Oct 8, 2014 at 14:36
  • @Roberthue I added it into the index.php's body and there basically is no other PHP in that file, so this is the only magic which is happening. This code is within a div.
    – user49869
    Oct 8, 2014 at 14:42
  • You are suppose to post it in loop. so post it in loop.php or content.php based on which theme you are using. Check EDIT 2.
    – Robert hue
    Oct 8, 2014 at 14:45
  • @Roberthue thanks again for the patience. It is inside the loop (like in your first answer), should have mentioned that. Still the same issue (look at my edit again, now its correct)
    – user49869
    Oct 8, 2014 at 14:57
  • It's working fine as intended. I just tested it on one of my website. For me it's showing a link with download text and URL of attached images and each URL is different. Try adding wp_reset_postdata(); before loop starts, means just before this if ( have_posts() ) : while (have_posts()) : the_post();
    – Robert hue
    Oct 8, 2014 at 15:08

2 Answers 2

1

You can get attachment URL like this.

<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
   <a href="<?php $featured_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); echo $featured_image_url[0]; ?>">
     <?php the_title(); ?>
   </a>
<?php endwhile; endif; ?>

First you will need to get the attachment ID so you will need to get it from get_post_thumbnail_id and then we can get image data array with wp_get_attachment_image_src by supplying the attachment ID. And will print URL of image from array.

Also this get_post_thumbnail_id will get attachment (image) ID of featured image. It does not depend on how many images you upload in a post. You must assign a featured image to a post.

EDIT:

If you want to print the link of PDF attachment only then you can use this code instead. What this will do is search for all PDF attachments of a post and then print the link of first PDF attachment only. So you don't need to worry about having more PDF files in a post.

<?php if ( have_posts() ) : while (have_posts()) : the_post();
    $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => null, 'post_parent' => $post->ID );
    $attachments = get_posts( $args );
    if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
          ?>
              <a href="<?php echo wp_get_attachment_url( $attachment->ID ); ?>"><?php echo get_the_title(); ?></a>
          <?php
        }
    }
endwhile; endif; ?>

EDIT 2

Post this inside WordPress loop or in content.php or loop.php

<?php
    $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_parent' => $post->ID );
    $attachments = get_posts( $args );
    if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
          ?>
              <a href="<?php echo wp_get_attachment_url( $attachment->ID ); ?>"><?php echo get_the_title; ?></a>
          <?php
        }
    }
?>
10
  • thanks for the answer. Just to make that clear, I don't want to insert an image as media file into each post. Its a PDF. Does this make any difference? edit: I get an empty href=""
    – user49869
    Oct 8, 2014 at 11:46
  • This will not work for PDF file because you can not set PDF file as featured images. So you will have to follow different approach. You will need to search through all attachments of a post and then print first PDF file. Check edits in above answer.
    – Robert hue
    Oct 8, 2014 at 11:59
  • thanks again for your help, I really appreciate it. It is working :) but I really need to ask you again, how I could make this for every file. Its not that I only use PDF's, I basically want to have it for every file type which is allowed in Wordpress.
    – user49869
    Oct 8, 2014 at 13:08
  • I updated code for each PDF file. Now all PDF files will be listed.
    – Robert hue
    Oct 8, 2014 at 13:13
  • sorry if I explained myself a bit bad, but I want this with every filetype of a post (means each post will have 1 file attached, but its not limited to a PDF). Thanks again, I appreciate that!
    – user49869
    Oct 8, 2014 at 13:17
0

With a little modification, it helped me a link to PDF files:

$args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts( $args );
if ( $attachments ) {
    foreach ( $attachments as $attachment )
    {
        if( strpos( wp_get_attachment_url( $attachment->ID ) , '.pdf') )
        {
            ?>
              <li><a href="<?php echo wp_get_attachment_url( $attachment->ID ); ?>"><?php echo 'mi archivo PDF' ?></a></li>
          <?php
        }

    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.