WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_permalink to attachment not working (2 posts)

  1. pawciak
    Member
    Posted 1 year ago #

    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?

  2. pawciak
    Member
    Posted 1 year ago #

    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>';
              }
         }
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic