Skip to main content
Updated code
Source Link
jw60660
  • 1.5k
  • 2
  • 14
  • 28

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' => 1418,
                               '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 wp_get_attachment_link'<a href=" ' . get_permalink( $post->ID ) . '">';
                                       echo wp_get_attachment_image($attachment->ID, 'thumbnail', true, '');
                                       echo '</a></li>';
                                       
                                   }
                               };
                               
                               endwhile;
                               
                               ?>   
                        

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:

<?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' => 14,
                               '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 wp_get_attachment_link($attachment->ID, 'thumbnail', true, '');
                                       echo '</li>';
                                   }
                               };
                               
                               endwhile;
                               
                               ?>   

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>';
                                       
                                   }
                               };
                               
                               endwhile;
                               
                               ?>   
                        
Source Link
jw60660
  • 1.5k
  • 2
  • 14
  • 28

Getting attachment post using wp_get_attachment_link

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:

<?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' => 14,
                               '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 wp_get_attachment_link($attachment->ID, 'thumbnail', true, '');
                                       echo '</li>';
                                   }
                               };
                               
                               endwhile;
                               
                               ?>   

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.