On my website, I have a custom post type (“foto’s”). It contains photo albums which are fetched from Facebook with Facebook Photo Fetcher (plugin). This works great. Unfortunately, this plugin does not allow for pagination. On pages that contain a lot of images this can cause problems (verrrrryyy long load times), such as here.
Because I am using a CPT it seems possible to me to use pagination. Unfortunately I have never used pagination before and I have no idea how to implement it. Is it in the declaration of the cpt, or is it a different function? And how could I make it work only when posts have more than X images?
My guess would be to create a template single-fotos.php
which differs from the default one in that it uses pagination. I do not know how to display pagination when there is more than X images though.
EDIT 1
Building on Matthew’s answer, I tried a couple of things. It seems better to edit the plugin, rather than trying building a custom post type.
I added the following lines:
// Look for Next page tag in starttag
if( preg_match('/next=(d+)/', $retVal['startTag'], $matches) ) $retVal['next'] =$matches[1];
...
// Add default value of next to defaults array
$defaults = array('cols' => 6, //Number of columns of images (aka Number of images per row)
...
'next' => 42); // Can be any number, but preferably a multitude of 'more'
...
// Output photos AND other tags such as more tag and next page
foreach($photos as $photo)
{
//Output this photo (must get rid of [], or WP will try to run it as shortcode)
...
$link = '<a rel="shadowbox[' . $aid . ']" class="fbPhoto" href="'.$photo->source . '" title="'.$caption_with_br.' " ><img src="' . $photo->picture . '" alt="" /></a>';
$retVal['content'] .= "<dl class='gallery-item' style="width:$itemwidth%">";
$retVal['content'] .= "<dt class='gallery-icon'>$link</dt>";
...
$retVal['content'] .= "</dl>n";
//Move on to the next row?
if( $params['cols'] > 0 && ++$i % $params['cols'] == 0 ) $retVal['content'] .= "<br style="clear: both" />nn";
//Insert a more tag?
if( $params['more'] > 0 && $i == $params['more'] ) $retVal['content'] .= "<!--more-->nn";
//Insert a next page tag?
if( $params['next'] > 0 && $i == $params['next'] ) $retVal['content'] .= "<!--nextpage-->nn";
}
if( $i%$params['cols'] != 0 ) $retVal['content'] .= "<br style="clear: both" />nn";
$retVal['content'] .= "</div>n";
The problem with this is, that the nextpage-tag is only rendered once! After 42 photos (default) the next-tag is inserted, but after the following 42 photos it isn’t rendered again. I suppose that is quite normal, but I am not familiar enough with PHP to accomplish that it is rendered every multiplication of the value given (or if not, default value) starting from n=1. How can this be done?
EDIT 2
Note that this should work in combination with a more
tag and that pagination oughtn’t show in the snippet. So I do not want in a snippet. Also comments and the respond field should only be placed on the last page.
Try
EDIT (explaination)
in the code posted by OP
$params['next'] > 0 && $i == $params['next']
assuming$i
is incremented every foreach cycle (this is not visible in the code, but it can be intuited) when$i > $params['next']
this not work and the next content is added once.In my code, using
$u = 0
after adding<!--nextpage-->nn
it should work because $u is incremented again by foreach so my codeif( $params['next'] > 0 && $u == $params['next'] ) {
should works for every multiple of $params[‘next’].I’m not familiar with this plugin, but in looking at the source, I think you could do something like this (EDIT: This doesn’t work, because the plugin bails if there is more than one tag per post, see below for a similar approach):
Then use page links to paginate the post.
The next step would be to add an action onto
wp_insert_post_data
before the plugin does (using a lower priority number, like 5) and intercept a more generic version of the tag (without pagination). Run an API lookup before the plugin does to find out the number of items, then programmatically break it up like above.Edit
As noted in the comments, the plugin doesn’t recognize more than one tag per post. Let’s take a different approach then. The “magic tag” it uses allows for columnizing. It counts the number of “cells” and inserts a
<br style="clear: both" />
. If you don’t need that line break (since you could use pure CSS to accomplish the same thing), you can replace it with the<!--nextpage-->
tag. Here’s an example of doing that:If you do need that line break, then count out the number of rows per page you want, and replace every nth line break with the nextpage tag.