0

i have a post width 10 attachment image in post

i want querylist 10 attachment in post as:

<li><a href="url-img-attachment-sizefull-img1" title=""><img src="url-img-attachment-sizethumb-img1"></li>
<li><a href="url-img-attachment-sizefull-img1" title=""><img src="url-img-attachment-sizethumb-img1"></li>
.....

Give any code.

My code add:

<?php query_posts($args); if (have_posts()) { while (have_posts()) { the_post(); ?>

<?php $args = array( 'post_type' => 'attachment', 'numberposts' => 3, 'orderby' => 'rand', 'order' => 'DESC', 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' => $post->ID ); 
$attachments = get_posts($args);
    if ($attachments) {
    foreach ( $attachments as $attachment ) { ?>
                        <a href="<?php echo wp_get_attachment_thumb_url( $attachment->ID,'large', false ); ?>" title="<?php the_title(); ?>"><img src="<?php echo wp_get_attachment_thumb_url( $attachment->ID,'large', false ); ?>" alt="<?php echo get_post_meta($attachment->ID, '_wp_attachment_image_alt', true) ?>" /></a>
<?php }} ?>
<?php }} wp_reset_query(); ?>

but <?php echo wp_get_attachment_thumb_url( $attachment->ID,'large', false ); ?> don't show image large it's image : thumbnail

how to fix it's

Thanks

1

wp_get_attachment_thumb_url() doesn't accept three parameters. It takes one-- the attachment ID. And, as you might guess from the name of the function, it return the thumbnail URL *_thumb_url. If you'd have simply checked the Codex entry for the function you'd have seen that.

If you'd check the Codex, you'd see several other things too

  1. Don't use query_posts()

    Note: This function isn't meant to be used by plugins or themes.

  2. numberposts has long since been deprecated. In fact, it may not work at all anymore as I can't find it mentioned in the Codex any longer, nor do I see it the source for WP_Query.

Let's correct this. The key function you want is wp_get_attachment_image_src(), but your code is an poorly formatted, unreadable mess. Let's fix that too.

  1. Use a new WP_Query object instead of query_posts() I don't know what arguments you are using as you didn't post that part.
  2. Use posts_per_page instead of numberposts (which name never made sense anyway)
  3. Use wp_get_attachment_image_src()
  4. Indent, and format, and organize. Save your sanity man!

Which gives us...

$args = array('post_type' => 'post'); // not sure what your actually are.
$images = new WP_Query($args);

if ($images->have_posts()) { 
  while ($images->have_posts()) { 
    $images->the_post();
    $args = array( 
      'post_type' => 'attachment', 
      'posts_per_page' => 3, 
      'orderby' => 'rand', 
      'order' => 'DESC', 
      'post_mime_type' => 'image' ,
      'post_status' => null, 
      'post_parent' => $post->ID 
    ); 
    $attachments = get_posts($args);
    if ($attachments) {
      foreach ( $attachments as $attachment ) { 
        $image = wp_get_attachment_image_src( $attachment->ID,'large', false ); 
        if (empty($image[0])) continue;
        $image_meta = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true); ?>
        <a href="<?php echo $image[0]; ?>" title="<?php the_title(); ?>">
          <img src="<?php echo $image[0]; ?>" alt="<?php echo $image_meta ?>" />
        </a><?php
      }
    }
  }
} 
wp_reset_postdata();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.