我试图从特定帖子(自定义帖子)中提取图片作为附件,
。。。我在WordPress codex中使用了这个例子codex example to display all images as a list
。。。除了那篇文章中的图片不断重复大约3到4次(我总共有7张图片附在那篇文章上)。无论我将numberposts设置为什么或post\\u per\\u页面设置为什么,我都会不断得到重复的图像,大约28张,而我应该只有7张。
如果我的numberposts是1或者我的post\\u per\\u image是1,那么我只会得到附加到该帖子的7张图片中的1张。。。我做错了什么?
下面是我正在使用的代码,下面是我的网站上发生的事情的一个示例my test site where i\'m trying to pull images from a post to use within FlexSlider
非常感谢您的帮助_辛迪
<ul class="slides">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$args1 = array(
\'post_type\' => \'attachment\',
\'p = 107\',
\'numberposts\' => 7,
\'post_status\' => \'inherit\'
);
$attachments = get_posts( $args1 );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo \'<li>\';
echo wp_get_attachment_image( $attachment->ID, \'home-slider\' );
echo \'</li>\';
}
}
endwhile; endif; ?>
</ul>
</div>
</div>
</div>
</div> <!-- end 1080 pixels Container -->
<div class="grid-gradient-bg"></div>
</div>
<!-- Slider Navigation -->
<div id="home-hero-nav" role="">
<div class="container"> <!-- 1080 pixels Container -->
<div class="full-width columns">
<ul class="slider-menu thumbnails clearfix">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$args2 = array(
\'post_type\' => \'attachment\',
\'p = 107\',
\'numberposts\' => 0,
\'post_status\' => \'inherit\'
);
$attachments = get_posts( $args2 );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo \'<li>\';
echo wp_get_attachment_image( $attachment->ID, \'home-slider-thumb\' );
echo \'</li>\';
}
}
endwhile; endif; ?>
</ul>
SO网友:ccbar
修复方法是将foreach循环从while循环中去掉。我还将附件设置为get\\u children。
希望这对别人有帮助!Also see this great post on premiumdw!!
<?php
$attachments = get_children(array(\'post_parent\' => [post id], \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'post_status\' => \'inherit\', \'posts_per_page\' => [however many images attached to your post] ));
if ($attachments) { // if there are images attached to posting, start the flexslider markup
foreach ( $attachments as $attachment_id => $attachment ) { // create the list items for images with captions
echo \'<li>\';
echo wp_get_attachment_image($attachment_id, \'[string or another array for width and height]\');
echo \'</li>\';
}
} // end see if images
wp_reset_postdata();
?>
\\u辛迪