WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] get_permalink to attachment not working

[Resolved] get_permalink to attachment not working

  • Hey,

    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?

Viewing 1 replies (of 1 total)
  • I’ve created by myself correct 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>';
              }
         }
    
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘[Resolved] get_permalink to attachment not working’ is closed to new replies.
Skip to toolbar