MidhuN
MidhuN

Reputation: 423

wp_get_attachment_url not working

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(); ?>
<?php 
$recent = new WP_Query("cat=7&orderby=title&order=ASC"); 
while($recent->have_posts()):$recent->the_post();
$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">
    <?php
        if ( has_post_thumbnail() )  { 
            the_post_thumbnail();
        }
    ?></a>
<a href = "" >  <?php if ( is_user_logged_in() ) {
        echo "Download";
     }?></a>
    <a href=" http://localhost/wordpress/login.php"> <?php if( !(is_user_logged_in()) )
    {
        echo "Please signup/login to download this file";
    }
    ?>
</a>

<div id="Download_post_description">
        <?php 
            if( is_array( $desc_values ) )
            {
                foreach($desc_values as $key => $value );
                echo "$value</n>"; 
            }
        ?>
    </div>
</div>
<?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?

Upvotes: 0

Views: 3439

Answers (1)

kjetilh
kjetilh

Reputation: 4976

  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()

-

<?php
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
    }
}
?>

Upvotes: 1

Related Questions