1

I'm trying to use Wordpress paginate function to only show 3 pages at most(client wants it this way) so that when you go up a page it shows the current page you're on and the previous and next page:

This is the result they want

Unfortunately when I do it I get several pages including the last page:

This is the result I get from my code

This is my current paginate code, please note the format is like this because I have Tabs that each need to be paged with different queries

echo '<div id ="nav_pages">'; 
    echo '<div class="prev_first"></div>'; 
    echo '<div class="pages">';
$pag_args1 = array(
'type'         => 'list',
'prev_next'       => False,
'end_size'        => 1,
'add_args'        => false,
'add_fragment'    => '',
'show_all' => false,
'base'         => '' . $url . '?paged1=%#%',                
    'format'  => '?paged1=%#%',
    'current' => $paged1,
    'total'   => $get_mining->max_num_pages);

    echo paginate_links( $pag_args1 );
    echo '</div>';
    echo '<div class="page_x_of_y2">Page <span>' . $paged1 . '</span> of <span>' . $get_mining->max_num_pages . '</span></div>';    
echo '</div>';

Is there anyway I can modify this code to look like the pagination in the first image rather than what it looks like in the second one? Do I need to write a custom paginate function to achieve this? I dont agree with what the client wants as I feel like the latter is a better option but you know what they say: "The customer is always right"

UPDATED PARTIAL SOLUTION

So after doing bit more reseach I found this link:Getting paginate_links()'end_size' to display 0 This solution almost allows me to achieve the goal I want here is my modified code:

echo '<div id ="nav_pages">'; 
    echo '<div class="prev_first"></div>'; 
    echo '<div class="pages">';
$pag_args1 = array(
'type'         => 'array',
'prev_next'       => False,
'end_size'        => 0,
'mid_size'        => 1,
'add_args'        => false,
'add_fragment'    => '',
'show_all' => false,
'base'         => '' . $url . '?paged1=%#%',                
    'format'  => '?paged1=%#%',
    'current' => $paged1,
    'total'   => $get_mining->max_num_pages);
    $paginate_links = paginate_links( $pag_args1 );
    $c=$pag_args1['current'];
    $allowed=[sprintf('/?paged1=%d',$c-1),'current',sprintf('?paged1=%d',$c+1)];
    $paginate_links=array_filter(
        $paginate_links,
    function( $value ) use ( $allowed ) {
        foreach( (array) $allowed as $tag )
        {
            if( false !== strpos( $value, $tag ) )
                return true;
        }
        return false;
    }
);

   if( ! empty( $paginate_links ) )
        printf("<ul class='page-numbers'>\n\t<li>%s</li>\n</ul>\n",join( "</li>\n\t<li>", $paginate_links));
    echo '</div>';
    echo '<div class="page_x_of_y2">Page <span>' . $paged1 . '</span> of <span>' . $get_mining->max_num_pages . '</span></div>';    
    echo '</div>';


}//Endif

This almost achieves what I want, the only problem now is that it wont show the previous page when I go above page 1. I'm getting Closer!!

1

Check the mid_size parameter in paginate_links

mid_size: How many numbers to either side of current page, but not including current page. Default 2.

So set it to: 'mid_size' => 1

Also you probably don't want to show the first and last page, so set it to 0 (it's 1 by default): 'end_size' => 0

Hope this helps.

-

EDIT:

It seems like the end property does not hide the last element. The easiest way is to hide it is using CSS:

.dots ~ .dots + .page-numbers { display: none; }

Otherwise, to remove the actual output from the function you would need to tweak it. You can return an array changing the type argument, loop through the elements and remove the one you are not interested in.

  • Close but it still shows the last page. 1 2 ... 48 Page 2 0f 48. Thanks for the help im getting closer I just need to not have the last page there. – Zayd Bhyat Nov 24 '18 at 6:49
  • @ZaydBhyat Are you sure you set 'end_size' => 0? It was set to 1 in the question code (make sure it is not twice in the array). – Alvaro Nov 24 '18 at 12:07
  • I’ll double check when I get into work today. I’m pretty sure it’s 0 – Zayd Bhyat Nov 26 '18 at 6:17
  • My end_size has been set to 0 and it is only in the array once. I feel I may need to write a custom function to get this right – Zayd Bhyat Nov 26 '18 at 6:59
  • So doing a bit more research i found this on wordpress stackexchange link. This was the closest for me. Now the only problem is its only showing the next page and not the previous page. @Alvaro can you check the post out and maybe help me what I want to achieve. Ive updated my question to include this source and the my modified code – Zayd Bhyat Nov 27 '18 at 11:07
0

Found the answer thanks to the link in the updated part of my question. Here is the solution to it:

echo '<div id ="nav_pages">'; 
    echo '<div class="prev_first"></div>'; 
    echo '<div class="pages">';
$pag_args1 = array(
'type'         => 'array',
'prev_next'       => False,
'end_size'        => 0,
'mid_size'        => 1,
'add_args'        => false,
'add_fragment'    => '',
'show_all' => false,
'base'         => '' . $url . '?paged1=%#%',                
    'format'  => '?paged1=%#%',
    'current' => $paged1,
    'total'   => $get_mining->max_num_pages);
    $paginate_links = paginate_links( $pag_args1 );
    $c=$pag_args1['current'];
    $allowed=[sprintf('?paged1=%d',$c-1),'current',sprintf('?paged1=%d',$c+1)];
    $paginate_links=array_filter(
        $paginate_links,
    function( $value ) use ( $allowed ) {
        foreach( (array) $allowed as $tag )
        {
            if( false !== strpos( $value, $tag ) )
                return true;
        }
        return false;
    }
);

   if( ! empty( $paginate_links ) )
        printf("<ul class='page-numbers'>\n\t<li>%s</li>\n</ul>\n",join( "</li>\n\t<li>", $paginate_links));
    echo '</div>';
    echo '<div class="page_x_of_y2">Page <span>' . $paged1 . '</span> of <span>' . $get_mining->max_num_pages . '</span></div>';    
    echo '</div>';

So what I had to do was:

  1. Make the pagination arguments into an array
  2. Store the resultant Array in a variable and store the current page in a variable
  3. Create another variable with allowed values, in this case its the current page and a page up and down on either side.
  4. Filter the array with a function that allows only the allowed values
  5. Finally display the pagination array if there are pages to display.

My solution looks like this now:

Solution I wanted and achieved

  • 1
    Great you could solve it. I posted an alternative using only CSS to hide the element, in case you are ok simply doing so, to avoid having the extra filter loop in PHP. – Alvaro Nov 27 '18 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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