我可以对这个GET_ATTACHATION查询进行分页吗?

时间:2012-01-20 作者:Dean Elliott

是的,基本上,我希望能够显示15个图像附加到某个页面,并在页面有15个以上的图像时进行分页。

我对这种问题不是很熟悉,所以我有点困惑。

 <?php $args = array(\'numberposts\' => 15, \'orderby\' => \'menu_order\', \'order\' => \'ASC\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'post_parent\' => $post->ID); ?>

                    <?php if ( $photos = get_posts($args) ) : ?>

                        <?php foreach ( $photos as $photo ) : ?>

                            <a href="<?php echo $photo->guid; ?>" target="_blank" class="lightbox" rel="[<?php echo $post->ID; ?>]"><?php echo wp_get_attachment_image($photo->ID,\'thumbnail\'); ?></a>

                        <?php endforeach; ?>

                    <?php endif; ?>

1 个回复
SO网友:Chris Cox

是的。您需要在查询中获取/设置$,如下所示:

<?php
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args = array(
  \'numberposts\' => 15, 
  \'orderby\' => \'menu_order\', 
  \'order\' => \'ASC\', 
  \'post_type\' => \'attachment\', 
  \'post_mime_type\' => \'image\', 
  \'post_parent\' => $post->ID, 
  \'paged\' => $paged
  ); ?>

                <?php $photos = new WP_Query($args);  ?>
                    <?php if ( $photos->have_posts() ) :  ?>
                      <?php while ( $photos->have_posts() ) : $photos->the_post(); ?>
                        <?php $url = wp_get_attachment_image_src($post->ID); ?>
                        <a href="<?php echo $url[0]; ?>" target="_blank" class="lightbox" rel="[<?php echo $post->ID; ?>]"><?php echo wp_get_attachment_image($post->ID,\'thumbnail\'); ?></a>

                    <?php endwhile; ?>
<?php
$big = 999999999; // need an unlikely integer

echo paginate_links( array(
\'base\' => str_replace( $big, \'%#%\', get_pagenum_link( $big ) ),
\'format\' => \'?paged=%#%\',
\'current\' => max( 1, get_query_var(\'paged\') ),
\'total\' => $photos->max_num_pages
) );
endif;
?>

结束