Christian Giupponi Christian Giupponi - 3 months ago 15
PHP Question

Wordpress - Can't paginate gallery results

I have a custom filter to add pagination to a gallery.

This is the code:

function mysite_custom_gallery($output, $attr)
{
global $post;

//GALLERY SETUP STARTS HERE----------------------------------------//
if (isset($attr['orderby'])) {
$attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
if (! $attr['orderby']) {
unset($attr['orderby']);
}
}

$order = 'ASC';
extract(shortcode_atts(array(
'order' => $order,
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));

$orderby = 'menu_order ID';

if (! empty($include)) {
$include = preg_replace('/[^0-9,]+/', '', $include);
$_attachments = get_posts(array('include' => $include,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $order,
'orderby' => $orderby
));

$attachments = array();
foreach ($_attachments as $key => $val) {
$attachments[$val->ID] = $_attachments[$key];
}
}
if (empty($attachments)) {
return '';
}
//GALLERY SETUP END HERE------------------------------------------//

//PAGINATION SETUP START HERE-------------------------------------//
$current = (get_query_var('paged')) ? get_query_var('paged') : 1;
$per_page = 24;
//$offset = ($page-1) * $per_page;
$offset = ($current - 1) * $per_page;
$big = 999999999; // need an unlikely integer

$total = sizeof($attachments);
$total_pages = round($total / $per_page);
if ($total_pages < ($total / $per_page)) {
$total_pages = $total_pages + 1;
}
//PAGINATION SETUP END HERE-------------------------------------//

//GALLERY OUTPUT START HERE---------------------------------------//
$output = "<div class=\"gallery-images\">\n";
$output .= '<div class="row">';
$counter = 0;
$pos = 0;
foreach ($attachments as $id => $attachment) {
$pos++;

if (($counter < $per_page) && ($pos > $offset)) {
$counter++;
$output .= '<div class="col-md-3 col-sm-4 col-xs-6 box-gallery">';
$largetitle = get_the_title($attachment->ID);
/*$largeimg = wp_get_attachment_image_src($id, 'full');*/
$linkimg = get_attachment_link($attachment->ID);
$img = wp_get_attachment_image_src($id, 'mlt-medium');
$output .= " <a href=\"$linkimg\" title=\"$largetitle\"><img src=\"$img[0]\" width=\"$img[1]\" height=\"$img[2]\" alt=\"\" /></a>\n";
$output .= '</div>';
}

}
$output .= "<div class=\"clear\"></div>\n";
$output .= "</div>\n";
$output .= "</div>\n";
//GALLERY OUTPUT ENDS HERE---------------------------------------//

//PAGINATION OUTPUT START HERE-------------------------------------//
$output .= '<div class="pagination">';
$output .= $paginate_links = paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link($big) ),
'current' => max( 1, get_query_var('paged') ),
'total' => $total_pages,
'mid_size' => 5
) );
$output .= "</div>\n";

//PAGINATION OUTPUT ENDS HERE-------------------------------------//

return $output;
}


And this is the filter:

add_filter('post_gallery', 'mysite_custom_gallery', 10, 2);


The gallery is correctly paginated every 24 images and event the pagination is correct, it shows the correct number of pages.

The problem is that if I click on a link nothing happen, and I can't understand why is not working.

The generated url for the second page seems correct:

http://mysite.app/photogallery/argentina/buenos-aires/buenos-aires/page/2/


but if I try to click or visit the link I'm redirected to:

http://mysite.app/photogallery/argentina/buenos-aires/buenos-aires/


so it remove the
page/2
segments.

Can please someone help me to find the issue?

Edit

here the pagination source:

<div class="pagination">
<span class="page-numbers current">1</span>
<a class="page-numbers" href="/photogallery/argentina/buenos-aires/buenos-aires/page/2/">2</a>
<a class="page-numbers" href="/photogallery/argentina/buenos-aires/buenos-aires/page/3/">3</a>
<a class="page-numbers" href="/photogallery/argentina/buenos-aires/buenos-aires/page/4/">4</a>
<a class="page-numbers" href="/photogallery/argentina/buenos-aires/buenos-aires/page/5/">5</a>
<a class="page-numbers" href="/photogallery/argentina/buenos-aires/buenos-aires/page/6/">6</a>
<a class="next page-numbers" href="/photogallery/argentina/buenos-aires/buenos-aires/page/2/">Next »</a>
</div>

Answer

Try to disable redirect for your post type, for example:

function redirect_canonical_off($redirect_url)
{
    if (is_singular('your_custom_post_type_slug')) {
        $redirect_url = false;
    }

    return $redirect_url;
}
add_filter("redirect_canonical", "redirect_canonical_off");

Make sure to replace your_custom_post_type_slug

Comments