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.


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\" />\n\n";
            //Insert a more tag?
            if( $params['more'] > 0 && $i == $params['more'] ) $retVal['content'] .= "<!--more-->\n\n";
            //Insert a next page tag?
            if( $params['next'] > 0 && $i == $params['next'] ) $retVal['content'] .= "<!--nextpage-->\n\n";
        if( $i%$params['cols'] != 0 ) $retVal['content'] .= "<br style=\"clear: both\" />\n\n";
        $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?


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 this in a snippet. Also comments and the respond field should only be placed on the last page.

2 Answers 2



$u = 0;
foreach($photos as $photo) {


  if( ($params['next'] > 0 && $u == $params['next']) ) {
    $retVal['content'] .= "<!--nextpage-->\n\n";
    $u = 0;

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-->\n\n it should work because $u is incremented again by foreach so my code if( $params['next'] > 0 && $u == $params['next'] ) { should works for every multiple of $params['next'].

  • This seems to work all right but it has two issues: pagination shows in a snippet even when the next tag is placed after the more tag. And comments are shown on every page of the multipage post, and I want them only on the last page. Commented Jul 29, 2013 at 14:08
  • But more is always passed. It makes sure that on an archive page only the 6 first photos are shown as an axcerpt. The problem then is that the pagination is shown there. Commented Jul 29, 2013 at 15:09
  • Sorry. I think this can be solved with an if ( ! is_archive() ) before you print the pagination and the comments template. But in your code I don't see where is the code that print pagination and comments. Maybe you can use this if statement also for limiting foreach($photos as $photo) { to loop only $param['more'] times
    – gmazzap
    Commented Jul 29, 2013 at 15:32
  • That won't work because the plugin works in such a way that it generates a HTML document as soon as you save a post. No PHP rendering of the plugin happens after that when you load a page. So is_archive won't work. Commented Jul 29, 2013 at 16:00
  • Now I have understand. (Sorry, I don't know the plugin) Can you give the full output of print_r($retVal) so I can work on it without installing the plugin?
    – gmazzap
    Commented Jul 29, 2013 at 16:17

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):

<!--FBGallery2 1234567890123456789 start=0 max=20 --><!--/FBGallery2-->


<!--FBGallery2 1234567890123456789 start=20 max=20 --><!--/FBGallery2-->


<!--FBGallery2 1234567890123456789 start=40 max=20 --><!--/FBGallery2-->


<!--FBGallery2 1234567890123456789 start=60 max=20 --><!--/FBGallery2-->


<!--FBGallery2 1234567890123456789 start=80 max=20 --><!--/FBGallery2-->

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.


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:

function wpse_100654_split_fb_album( $content ) {
    return str_replace( '<br style="clear: both" />', '<!--nextpage-->', $content );
add_action( 'wp_insert_post_data', 'wpse_100654_split_fb_album', 15 );

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.

  • This all sounds like a good plan, but I don't think I can implement this by myself without some extra explanation. The action would go in functions.php I suppose, but I have no idea what to do in that action. Also, your example code won't work because the plugin only supports one gallery per post. Commented May 25, 2013 at 9:57
  • Thanks for clarifying @BramVanroy. Check out my edits above for an even easier solution. Commented May 25, 2013 at 18:24
  • I completely looked over this! I have been busy with this, but this doesn't work. I cannot leave out the clear-tag (responsive design that completely relies on that clear tag), but I think there is an easy solution to my problem. See my edit in the main post. Thanks again! Commented Jul 29, 2013 at 12:41
  • @BramVanroy Before you start editing the plugin, have you tried using self-clearing nodes instead of a clear tag? google.com/search?q=clearfix Commented Jul 29, 2013 at 13:41
  • If you look at the source code of this page: denotenbalkers.be/fotos/2013-fietstocht-paalse-plas You can see that the plugin does not generate containers for each row. This makes it hard to apply any clearfix solution that works crossbrowser. If you do know one, please do share. Commented Jul 29, 2013 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.