349 posts
  • Bought between 100 and 499 items
  • Has been a member for 5-6 years
  • United Kingdom
kops says

Hi,

I’m trying to create a page template that shows the post title, excerpt and a link to the attachment.

I’ve been using this code:

<?php $args = array(
    'post_type' ?> 'attachment',
    'numberposts' => null,
    'post_status' => null,
    'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $attachment) {
        echo apply_filters('the_title', $attachment->post_title);
        the_attachment_link($attachment->ID, false);
    }
}?>

which is great and pulls the post attachment but it uses the document title as the link name like this:

POST TITLE / POST EXCERPT / DOCUMENT23 .DOC

How do I achieve this:

POST TITLE / POST EXCERPT / CLICK HERE TO DOWNLOAD

Any help much appreciated,

Thanks,

Jon.

349 posts
  • Bought between 100 and 499 items
  • Has been a member for 5-6 years
  • United Kingdom
kops says

Jsut to note – this is a list of posts, not a single post page.

2441 posts
  • Has been a member for 5-6 years
  • Exclusive Author
  • Europe
  • Bought between 10 and 49 items
  • Referred between 100 and 199 users
  • Sold between 100 and 1 000 dollars
  • Envato Studio (Microlancer) Beta Tester
digitalimpact says

Perhaps something like this to output the link?


foreach ($attachments as $attachment) {
        echo '<a href="' . wp_get_attachment_link($attachment->ID) . ' ">Click here to download</a>';
    }
349 posts
  • Bought between 100 and 499 items
  • Has been a member for 5-6 years
  • United Kingdom
kops says

Thanks for the reply!

It’s a step in the right direction – the links now show as specified text rather than the document title.

The URL behind the link, though, looks like this:

http://www.onslow.sch.uk/staff/%3Ca%20href=%27http://www.onslow.sch.uk/staff/wp-content/uploads/2011/09/doc2.doc%27%20title=%27doc2%27%3Edoc2%3C/a%3E

Any ideas how to strip that back to the straight attachment URL ?

Thanks again,

Jon.

349 posts
  • Bought between 100 and 499 items
  • Has been a member for 5-6 years
  • United Kingdom
kops says

apols – like this:

<a href="<a href='http://www.onslow.herts.sch.uk/staff/wp-content/uploads/2011/09/doc1.doc' title='doc1'>doc1</a> ">Click here to download</a>
2441 posts
  • Has been a member for 5-6 years
  • Exclusive Author
  • Europe
  • Bought between 10 and 49 items
  • Referred between 100 and 199 users
  • Sold between 100 and 1 000 dollars
  • Envato Studio (Microlancer) Beta Tester
digitalimpact says

Sorry about that, forgot that the function outputs the HTML , not just the URL .

So, revised:


foreach ($attachments as $attachment) {
echo wp_get_attachment_link( $attachment->ID, 'Click here to download' );        
    }

More info: http://codex.wordpress.org/Function_Reference/wp_get_attachment_link

349 posts
  • Bought between 100 and 499 items
  • Has been a member for 5-6 years
  • United Kingdom
kops says

Hi Again,

now it outputs:

<a href="http://www.onslow.sch.uk/staff/wp-content/uploads/2011/09/doc2.doc" title="doc2">doc2</a>

rather than

<a href="http://www.onslow.sch.uk/staff/wp-content/uploads/2011/09/doc2.doc" title="doc2">Click Here to Download</a>

Your help’s really appreciated, thanks.

J

2441 posts
  • Has been a member for 5-6 years
  • Exclusive Author
  • Europe
  • Bought between 10 and 49 items
  • Referred between 100 and 199 users
  • Sold between 100 and 1 000 dollars
  • Envato Studio (Microlancer) Beta Tester
digitalimpact says

Try specifying all arguments, like this:


echo wp_get_attachment_link( $attachment->ID, '' , false, false, 'Click here to download' );

I thought leaving everything else out might work, but the function receives 2 strings as arguments; guess it thinks the text is actually the first argument… Look at the Codex link I gave you and adapt to your needs :)

349 posts
  • Bought between 100 and 499 items
  • Has been a member for 5-6 years
  • United Kingdom
kops says

woo that works – I think it’s time for me to knuckle down and learn php rather than trying to fudge it each time!

you’re a star – thanks!

J

by
by
by
by
by
by