1

I want to show images in my single-content.php file. I have almost 20 to 25 images in every post. I want to print them with below code one by one. I have to change one line in code for every image for uniqueness so that's why I need to print them one by one not all in one loop.

I have code but I'm unable to add option for next image to print. I mean when I copy and paste the same code same image is printed again. So basically I need help here. How can I get next image when I paste code again and again for 20 time or 25 times to get all images. Hope I can describe my problem. (Sorry for my bad english.)

/* ATTACHMENT*/

  $args = array(
        'numberposts' => -1,
        'order' => 'ASC',
        'post_mime_type' => 'image',
        'post_parent' => $posid,
        'post_status' => null,
        'post_type' => 'attachment',
  );

  $atts = get_children( $args );

  $tatts = count($atts);
  $i = 1;

  foreach ($atts as $at)    
      $atid = $at->ID;
      $mimg = wp_get_attachment_image_src($atid,'large');
      $msrc = $mimg[0];
      $mw = $mimg[1];
      $mh = $mimg[2];
      $atslug = $at->post_name;
      $aturl = $surl.'/'.$poslug.'/'.$atslug.'/';
      echo '  <img class="mdi" src="'.$msrc.'"   />  ';

In other way I need a code that can display images in my post one by one (I know I can insert images in my post, but I want to just upload images to post and my code fetch images for me as I have to apply a php code for images as above code do same the code fetch images that attached to post ) like in my file template I put code like this, for ex: my all posts has 3 images so I want code like this:

<?php image 1 code here?>
<?php image 2 code here?>

<?php image 3 code here ?>
0

I'm afraid that your code doesn't make much sense (especially the part with foreach loop...), but here's how it can look like...

$attachments = get_children( array(
    'numberposts' => -1,
    'order' => 'ASC',
    'post_mime_type' => 'image',
    'post_parent' => $posid,
    'post_type' => 'attachment',
) );

// print image 0
if ( array_key_exists( 0, $attachments ) ) {
    $mimg = wp_get_attachment_image_src( $attachments[0]->ID, 'large' );
    $msrc = $mimg[0];
    $mw = $mimg[1];
    $mh = $mimg[2];
    $atslug = $at->post_name;
    $aturl = $surl . '/' . $poslug . '/' . $atslug . '/';
    echo '<img class="mdi" src="' . $msrc . '"   />';
}

// print image <NUMBER>
if ( array_key_exists( <NUMBER>, $attachments ) ) {
    $mimg = wp_get_attachment_image_src( $attachments[<NUMBER>]->ID, 'large' );
    $msrc = $mimg[0];
    $mw = $mimg[1];
    $mh = $mimg[2];
    $atslug = $at->post_name;
    $aturl = $surl . '/' . $poslug . '/' . $atslug . '/';
    echo '<img class="mdi" src="' . $msrc . '"   />';
}

PS. I'm not entirely sure what this part of code is for:

$msrc = $mimg[0];
$mw = $mimg[1];
$mh = $mimg[2];
$atslug = $at->post_name;
$aturl = $surl . '/' . $poslug . '/' . $atslug . '/';
echo '<img class="mdi" src="' . $msrc . '"   />';

because you don't use most of these variables ($mw, $mh, $atslug, $aturl), but I guess it can be only part of your code, so I'll leave them... But if you really don't use them, then you should remove them from that code...

Hope it'll be helpful :)

  • If it doesn't show anything, then if ( array_key_exists( 0, $attachments ) ) { part is false most probably, so there is a problem with your get_children args, I guess... If any attachment exists, then it should print the img tag. – Krzysiek Dróżdż Nov 25 '18 at 10:56

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.