I have this code that shows all images attached to my post-type.
Is there a way to paginate those? I have like 25 pics and I wan’t to avoid scroll as much as I can.
Code:
<div class="galleries ten columns">
<?php
$attachments = get_posts( array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'posts_per_page' => -1,
'post_status' => 'any',
'post_parent' => $post->ID)
);
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, full);
$html = '<a class="fancybox" href="'.$src[0].'">';
$html .= wp_get_attachment_image( $attachment->ID, 'gallery-thumb') .'</a>';
echo $html;
}
}
?>
</div>
Thanks!
I’m assuming you want something like this:
In this setup your the post content stays the same, appears to be static, while the gallery is paged. This means you have the main query, showing you the title, content and so on, and a secondary query, which is responsible for showing you the gallery images. The page links on this page are connected to the secondary loop and do the paging there. To achieve this I suggest using
WP_Query
instead ofget_posts()
, not only because I don’t have a solution for the latter in mind, but also because I think it’s generally the better approach. In a first step you have to setup a new query variable, which will be used for the pagination. The functionadd_gallery_query_var()
goes into yourfunctions.php
.Code:
Additionally, if you want pretty permalinks to work with the new query variable, you have to implement a new rewrite rule. For this add the following to your
functions.php
, which makes use of add_rewrite_tag and add_rewrite_rule.Code:
The next step is to create the secondary loop for the gallery.
WP_Query
is used to do so, additionallypaginate_links
gives us the possibility to create the links for paging through the gallery. I’ve decided to create a function for this, so below code goes into thefunctions.php
.Code:
As you can see the secondary query uses an extra post object –
$gallery
– and the query variable defined before –gallery_page
– to enable proper pagination. The latter is used to setup theformat
parameter ofpaginate_links()
. We shouldn’t forget to reset –wp_reset_postdata()
– to prevent problems. Of course this is just the secondary loop, not the complete template.I call the secondary attachment gallery query in my
single.php
, after the main query/loop, via the functionwpse124169_get_attachment_gallery()
. Following above steps you have your paginated attachment gallery set up. I’ve tested this and it’s working for me.Below I list the most important information and sources, if you’re interested in more details this should get you more then just started.
Information:
WP_Query
paginate_links
Optional:
What it does is, it changes the default link for the first image. Normally it is
http://site.ext/?p=123
orhttp://site.ext/post-name/
, the attachment parents permalink. That’s ok, it shows you the first image by default. But if you want the link for the first image represent the gallery query var in the URL, you have to hook intopaginate_links
. This leads to the link being condtsructed like thishttp://site.ext/?p=123&gallery_page=1
orhttp://site.ext/post-name/gallery/image/1
. This only applies to the navigation via the link bypaginate_links()
, calling the parent post does only show the according parent permalink. The code would also go into thefunctions.php
.Code:
Update:
This can be used for altering the [gallery] shortcode, like shown here: How to paginate wordpress [gallery] shortcode?.