WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a template file which shows the posts of a category called downloads. for each posts i have attached a pdf file. I have given a link to download the pdf file on the page. But when i click the download link it goes to the post page and from there i have to click to download the file. Is there any way to directly download without going to the post. ? I have tried using wp_get_attachment_url as the hyper-reference.but it is not working.The code that i have used is below:

<?php /*
Template Name: Downloads Template
<?php get_header(); ?>
$recent = new WP_Query("cat=7&orderby=title&order=ASC"); 
$desc_values = get_post_custom_values("description");
<div id="download_featured_image" class="<?php the_ID(); ?> download_image_title_desc">

    <a href="<?php the_permalink() ?>" rel="title">
        if ( has_post_thumbnail() )  { 
<a href = "" >  <?php if ( is_user_logged_in() ) {
        echo "Download";
    <a href="http://localhost/wordpress/login.php "> <?php if( !(is_user_logged_in()) )
        echo "Please signup/login to download this file";

<div id="Download_post_description">
            if( is_array( $desc_values ) )
                foreach($desc_values as $key => $value );
                echo "$value</n>"; 
<?php endwhile ?>
<?php get_footer(); ?>

I want to give the link to the uploaded pdf in the href which i have left blank. Can someone help me?

share|improve this question
Are you sure you have used wp_get_attachment_url not get_attachment_link ? – david.binda Mar 7 '13 at 12:45
up vote 0 down vote accepted
  1. Add the PDF attachment ID as a custom field value, for example attached_pdf_id.

  2. Get the URL using wp_get_attachment_url():

if ( is_user_logged_in() ) {

$pdf_link = wp_get_attachment_url( get_post_meta( get_the_ID(), 'attached_pdf_id', true ) );

if ( $pdf_link ) {
?><a href = "<?php echo $pdf_link ?>" >Download</a><?php
} else {
?>Sorry, no link available. Please contact the webmaser.<?php
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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