WordPress Query For Attachments
-
I have the following query
<?php // gets attached images $counter = 3; $args = array( 'post_type' => 'attachment', 'numberposts' => -1, // posts_per_page doesn't work for get_posts! 'orderby' => 'rand', 'caller_get_posts' => 1, 'post_mime_type' => 'image', 'post_status' => null, 'post_parent' => null, // any parent ); $attachments = get_posts($args); if ($attachments) { echo '<ul>'; // count number of available images and change if less than the specified limit if ( count($attachments) < $counter ) $counter = count($attachments); foreach ($attachments as $post) { if ( $counter > 0 ) { setup_postdata($post); $image = wp_get_attachment_image_src( $post->ID, 'thumbnail', false ); echo '<li class="media"><a href="'.get_permalink().'" title="'.get_the_title().'" style="background-image: url('.$image[0].');">'.get_the_title().'</a></li>'; $counter--; } } echo '</ul>'; } ?>
What I am unable to figure out on how to add a custom_post_type that i have created to it.
Thanks
Viewing 2 replies - 1 through 2 (of 2 total)
-
I am guessing you want to get attachments from all posts from your custom post type? Although you mention attached images so maybe you are talking about all attachments on a single custom type post. If you could be a bit more clear, I might have some example code for your needs.
I got this working on my own.
Thanks though<?php remove_all_filters('posts_orderby'); query_posts('showposts=3&post_type=image&orderby=rand'); global $more; $more=0;?> <?php if (have_posts) : while (have_posts()) : the_post(); global $more; $more=0;?> <?php $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'orderby' => 'rand', 'post_parent' => $post->ID ); $attachments = get_posts($args); if ($attachments) { echo ''; // count number of available images and change if less than the specified limit foreach ($attachments as $post) { setup_postdata($post); $image = wp_get_attachment_image_src( $post->ID, 'thumbnail', false ); echo '<span class="media"><a href="'.get_permalink().'" title="'.get_the_title().'" style="background-image: url('.$image[0].');">'.get_the_title().'</a></span>';; } echo ''; } ?> <?php endwhile; endif; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘WordPress Query For Attachments’ is closed to new replies.