I’m having some trouble with this sample widget code. I want to get all images (Minus the post thumbnail) from a page called “Gallery” but for some reason this is pulling all uploaded images from the entire site.
Also, how would I go about excluding the post thumbnail from this query?
query_posts('pagename=gallery');
if (have_posts()) :
echo "<ul class='recentwidget group photowidget'>";
while (have_posts()) : the_post();
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li class="left imageshadow photolarge">';
echo wp_get_attachment_image( $attachment->ID, 'full' );
echo '</li>';
}
}
endwhile;
endif;
wp_reset_query();
Use get_children
I used this code to extract all the images from a page gallery in the chosen order. you can include this code in the loop or use it stand alone. just choose the appropriate post_parent code (see bellow the code example).
This example show all images associated to the page id 1, have a look:
Find the post id you wan to extract images from and insert it into this argument:
'post_parent' => 1
(replace the 1 with your page id)you can also use:
If you want to use get_children in a loop, and get the post id from the returned post id.
If you want to exclude the image selected as a featured image i would have a
if
statement check if the image URL is equal to the featured image URL.Hope this helps! 🙂
Easiest way is to use
get_attached_media()
. There are many different ways, almost all of them covered here