admin管理员组文章数量:1122846
I am trying to pull images as attachments from a particular post (custom post),
...and I'm using the example within WordPress codex codex example to display all images as a list
...except the images in that post keep repeating about 3 or 4 times (I have a total of 7 images attached to that post). no matter what I set the numberposts to or the post_per_page, I keep getting repeated images, about 28 when I should only have the seven.
If my numberposts is 1 or my post_per_image is 1 I only get 1 image of the 7 attached to that post... what am I doing wrong?
The code I'm using is below, and an example of what's happening on my site is here my test site where i'm trying to pull images from a post to use within FlexSlider
any help is very much appreciated. _Cindy
<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>
I am trying to pull images as attachments from a particular post (custom post),
...and I'm using the example within WordPress codex codex example to display all images as a list
...except the images in that post keep repeating about 3 or 4 times (I have a total of 7 images attached to that post). no matter what I set the numberposts to or the post_per_page, I keep getting repeated images, about 28 when I should only have the seven.
If my numberposts is 1 or my post_per_image is 1 I only get 1 image of the 7 attached to that post... what am I doing wrong?
The code I'm using is below, and an example of what's happening on my site is here my test site where i'm trying to pull images from a post to use within FlexSlider
any help is very much appreciated. _Cindy
<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>
Share
Improve this question
edited Mar 17, 2015 at 0:00
ccbar
asked Mar 16, 2015 at 23:55
ccbarccbar
151 gold badge2 silver badges12 bronze badges
2 Answers
Reset to default 0Your second arg "p = 107" isn't valid.
What you could try is the get_attached_media() function.
And do something like this:
$media = get_attached_media('image', 2360);
foreach ($media as $m) {
echo '<li>';
echo wp_get_attachment_image( $m->ID, 'home-slider' );
echo '</li>';
}
Here's the fix - taking the foreach loop out of the while loop. I also set attachments as get_children.
Hope this helps someone! 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();
?>
_Cindy
本文标签: How to query images from a post to use in a slider
版权声明:本文标题:How to query images from a post to use in a slider 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736284392a1927240.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论