361 posts
  • Has been part of the Envato Community for over 5 years
  • Has collected 100+ items on Envato Market
  • Located in 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.

361 posts
  • Has been part of the Envato Community for over 5 years
  • Has collected 100+ items on Envato Market
  • Located in United Kingdom
kops says

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

2459 posts
  • Has been part of the Envato Community for over 5 years
  • Has sold $100+ on Envato Market
  • Has referred 100+ members
  • Has been a beta tester for an Envato feature
+3 more
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>';
    }
361 posts
  • Has been part of the Envato Community for over 5 years
  • Has collected 100+ items on Envato Market
  • Located in 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.

361 posts
  • Has been part of the Envato Community for over 5 years
  • Has collected 100+ items on Envato Market
  • Located in 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>
2459 posts
  • Has been part of the Envato Community for over 5 years
  • Has sold $100+ on Envato Market
  • Has referred 100+ members
  • Has been a beta tester for an Envato feature
+3 more
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

361 posts
  • Has been part of the Envato Community for over 5 years
  • Has collected 100+ items on Envato Market
  • Located in 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

2459 posts
  • Has been part of the Envato Community for over 5 years
  • Has sold $100+ on Envato Market
  • Has referred 100+ members
  • Has been a beta tester for an Envato feature
+3 more
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 :)

361 posts
  • Has been part of the Envato Community for over 5 years
  • Has collected 100+ items on Envato Market
  • Located in 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