Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have some code showing an image attached to a post, an image in a li list, works fine shows the image as its should.

I need the image to link through its main single post though and have tried the code below.

<?php
            $args = array(
               'post_type' =>  'attachment',
               'numberposts' =>  -1,
               'post_status' =>  null,
               'post_parent' =>  $post->ID
              );

              $attachments = get_posts( $args );
                 if ( $attachments ) {
                    foreach ( $attachments as $attachment ) {
                       echo '<li>  <a href="<?php the_permalink() ?>" title="Permanent Link to<?php the_title_attribute(); ?>">';
                       echo wp_get_attachment_image( $attachment->ID, 'full' );
                       echo '</a></li>';
                      }
                 }
?>

Unfortunately this doesn't print the permalink into the link. :(

Any ideas can it be done?

Thanks for looking.

share|improve this question
    
could you share your solution. I am having the same issue –  alex Mar 27 '12 at 14:05
add comment

1 Answer

up vote 1 down vote accepted

Jez, unpatient Jez :)

<?php
        $args = array(
           'post_type' =>  'attachment',
           'numberposts' =>  -1,
           'post_status' =>  null,
           'post_parent' =>  $post->ID
          );

          $attachments = get_posts( $args );
             if ( $attachments ) {
                foreach ( $attachments as $attachment ) {
                   echo '<li>  <a href="'. get_permalink($attachment->ID) .'" title="Permanent Link to<?php the_title_attribute(); ?>">';
                   echo wp_get_attachment_image( $attachment->ID, 'full' );
                   echo '</a></li>';
                  }
             }
?>

tried it, worked for me.

share|improve this answer
    
Thank you :D I need it to link to the permalink of the actual post the image is attached to though, the single.php as it were. The code above links through to the image link... –  Jezthomp Jan 30 '12 at 9:59
    
Sorted, thank you. –  Jezthomp Jan 30 '12 at 10:03
    
@ungestaltbar this title="Permanent Link to<?php the_title_attribute(); ?> is really neat. I've been looking for a better way to title my links for awhile -- this is so simple I should have known. Thanks –  David Mar 27 '12 at 15:14
add comment

Your Answer

 
discard

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.