admin管理员组文章数量:1130349
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 ?>
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 ?>
Share
Improve this question
edited Nov 25, 2018 at 8:43
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Nov 25, 2018 at 6:54
deepdeep
111 bronze badge
1 Answer
Reset to default 0I'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 :)
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 ?>
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 ?>
Share
Improve this question
edited Nov 25, 2018 at 8:43
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Nov 25, 2018 at 6:54
deepdeep
111 bronze badge
1 Answer
Reset to default 0I'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 :)
本文标签: phpHow to get next image with this code from same post
版权声明:本文标题:php - How to get next image with this code from same post? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749152463a2324053.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论