我正在抓取上传到该帖子的图像,并使用此功能:wp_get_attachment_image_src
<?php
$images = get_children( array( \'post_parent\' => $post->ID, \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'orderby\' => \'menu_order\', \'order\' => \'ASC\', \'numberposts\' => 2 ) );
if ( $images ) :
$total_images = count( $images );
$image = array_shift( $images );
$image_img_tag = wp_get_attachment_image_src( $image->ID, \'full\' );
?>
<div class="two_images">
<img src="<?php echo $image_img_tag[0] ?>">
</div>
如何获取上传到帖子的前两幅图像?我想我需要一份foreach声明的帮助。。并将其限制为两个。我试过了,但它只是一遍又一遍地打印出相同的第一张图像。。
<?php foreach ($image as $images) {
echo "<img src=\'$image_img_tag[0]\'>";
} ?>
如果我回音
$total_images
然后我得到2的正确计数
Here is the paste of the page
最合适的回答,由SO网友:Paul Sheldrake 整理而成
看起来您可能没有循环设置,请尝试此设置
<div class="two_images">
<?php
global $post;
$args = array(
\'post_parent\' => $post->ID,
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'numberposts\' => 2 );
$images = get_posts($args);
if ( $images ) {
$i = 0;
while($i <= 1){
echo wp_get_attachment_image( $images[$i]->ID, \'full\' );
$i++;
}
}
?>
</div>