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 following code

    <?php

      $args = array(
   'post_type' => 'attachment',
   'numberposts' => 12,
   'post_status' => null
  );

  $attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           echo '<li><a href="'.get_permalink( $attachment->ID ).'">';
           echo wp_get_attachment_image( $attachment->ID, array('100', '100') );
          echo '</a></li>';
          }
     }

?>

The point of this script is to show last added 12 photos (thumbs of it). And this works perfect. But I want to add second funcionality - link to the page where it comes from (usually native gallery embed into post/page)

The problem is that in this case the link is corrupted. It always links to the very first post. What I am doing wrong?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

The Problem is that you query for Post Status null. Try setting this to publish or inherit, because get_permalink is a function for published items, not for any post status.

This is because Permalinks to items with a status like draft should not be shown to the user.

share|improve this answer
    
THX fischi. I've found the problem. –  Paweł Skaba Feb 25 '13 at 22:58
add comment

Final version

<?php

      $args = array(
   'post_type' => 'attachment',
   'numberposts' => 12,
   'post_status' => null
  );

  $attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           $url = get_permalink( $attachment->ID );
          echo '<li><a href="'.strstr($url, '/attachment', true).'">';
           echo wp_get_attachment_image( $attachment->ID, array('100', '100') );
          echo '</a></li>';       
          }
     }

?>

where /attachment is everything NOT needed in the URL.

share|improve this answer
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.