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

The WordPress codex on wp_get_attachment_link says you can use the $permalink parameter to link to the page, not the image. But I would like to link to the post instead. I'm trying to get a list of images using the following (updated, see answers below):

<?php $new_query = new WP_Query('&showposts=1'); ?>
                        <?php while ($new_query->have_posts()) : $new_query->the_post(); ?>
                        <?php $args = array (
                               'post_type' => 'attachment',
                               'numberposts' => 18,
                               'orderby' => 'date',
                               'status' => 'publish',
                               'post_mime_type' => 'image',
                               'parent' => $post->ID
                        ); ?>
                        <?php $attachments = get_posts($args);
                               if ($attachments) {
                                   foreach ($attachments as $attachment) {
                                       echo '<li>';
                                       echo '<a href=" ' . get_permalink( $post->ID ) . '">';
                                       echo wp_get_attachment_image($attachment->ID, 'thumbnail' );
                                       echo '</a></li>';




Having trouble getting the images to link to the post. I'm not sure how to filter this to get the post, not the page.

share|improve this question
up vote 1 down vote accepted

Yes, if you pass third ($permalink) parameter as true in wp_get_attachment_link() image will link to attachment's page not image itself. You can use wp_get_attachment_link filter to change behavior of that parameter, but in this case it simply means overwriting whole function. Instead I would use wp_get_attachment_image() and get_permalink()


Maybe wp_get_attachment_image() would fit in this situation better. Here's also how your snipped would look like:

if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
       echo '<li>';
       echo '<a href="' . get_permalink( $post->ID ) . '">';
       echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );
       echo '</a></li>';
share|improve this answer
Thanks for the reply. I would, however, like the image to link to the post and not the image page or image. Sorry if I was confusing in my question. – jw60660 Mar 10 '12 at 13:07
You're welcome. Yes I know that, that's why you'll need get_permalink(), see update answer. – Mamaduka Mar 10 '12 at 13:37
If you don't mind me asking, if I wanted to pull attachments from a network, what changes would I make to this attachment loop? – jw60660 Mar 10 '12 at 14:06
As network you mean WordPress Multi-Site? – Mamaduka Mar 10 '12 at 14:19
Yes, multisite, thank you. By the way, what you recommended is resulting in the images pulling nicely, but the images link to one post on the main site in my multisite installation. I am posting the exact code I used above. Thanks! – jw60660 Mar 10 '12 at 14:30

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.