• Morning,

    I need some guidance on some WP code. I’m wondering if it’s possible to take this code (which is grabbing all the images from the posts across my site and dumping them out):

    $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null );
        $attachments = get_posts( $args );
        if ($attachments) {
    	foreach ( $attachments as $post ) {
    		setup_postdata($post);
    		the_attachment_link($post->ID, true);
    		the_excerpt();
    	}
        }

    and use the query_posts(); function in WP instead so I could create some pagination for all the images.

    Thanks in advance,
    R

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with a new WP_query. get_posts() makes use of the WP_Query class to fetch posts.
    http://codex.wordpress.org/Function_Reference/WP_Query

    Thread Starter richgcook

    (@richgcook)

    I seem to have got something working with this below, but it’s pulling in one post, infinitely (see here: http://goo.gl/4IlWG)

    <?php $ID = get_the_ID();
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	query_posts("order=DESC&orderby=post_date&posts_per_page=8&paged=$paged"); while ( have_posts() ) : the_post(); ?>
    
    		<?php
    		$args = array( 'post_type' => 'attachment', 'post_status' => null, 'post_parent' => null, );
    		$attachments = get_posts( $args );
    		if ($attachments) {
    			foreach ( $attachments as $post ) {
    				setup_postdata($post);
    				the_attachment_link($post->ID, true);
    				the_excerpt();
    			}
    		}
    		?>
    
    	<?php endwhile; ?>
    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this:

    foreach ( $attachments as $attachment ) {
    				//setup_postdata($post);
    				the_attachment_link($attachment->ID, true);
    				//the_excerpt();
    			}

    Thread Starter richgcook

    (@richgcook)

    Thanks for your help with this, keesiemeijer.

    Still the same problem – you can see it on that site again.

    Moderator keesiemeijer

    (@keesiemeijer)

    The default view is working so it must be something in your javascript.

    Thread Starter richgcook

    (@richgcook)

    It’s the grid view I’m looking at (http://itsfoolsgold.com/grid/) the default and grid views both use different templates.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘WordPress attachments in query_posts…’ is closed to new replies.