4

I need a pagination for the output that is generated by a WordPress plugin. The plugin retrieves all products of a certain productgroup from the datebase. The basic code is:

<?php 
    foreach ((array)$this->view['data']['produkte'] as $p) 
    { ?>
    ...some html code here
    } 
?>

where $p is an multi-dimensional array that contains the data for the single product. I'd like to limit the output to, say, 10 products and create a simple pagination for that. The plugin is quite complex and uses nested templates, therefore a custom query is probably not an option in this case.

Would be great if somebody could point me in the right direction. Thank you!

Here's the final code. Thank you very much for your valuable help!!

<?php
$nb_elem_per_page = 3;      
$page = isset($_GET['seite'])?intval($_GET['seite']-1):0;
$data = (array)$this->view['data']['produkte'];
$number_of_pages = intval(count($data)/$nb_elem_per_page)+2;
$page_no = $_REQUEST['seite'];
foreach (array_slice($data, $page*$nb_elem_per_page, $nb_elem_per_page) as $p) 
  { ?> some HTML here... <?php } ?>
<?php if (count($data) > $nb_elem_per_page) { ?>       
<ul id='paginator'>
  <?php
  for($i=1;$i<$number_of_pages;$i++){
  if ($i == $page_no) {?>
    <li><?php echo $i ?></li>
    <?php }
    else { ?>
    <li><a href="<?php echo get_permalink(); ?>?show=<?php echo $_REQUEST['show']; ?>&seite=<?=$i?>"><?php echo $i ?></a></li>
  <?php }} ?>
</ul>
<?php { ?>

Since the default url contains already a query string, I had to modify the code a litte bit. I don't want the current site to have a link in the pagination. This gets me the number of the pagination query string:

$page_no = $_REQUEST['seite'];

I can then easily change the pagination links with a simple if-statement:

if ($i == $page_no) {...}?>

Thanks again!

user2516117
  • 129
  • 2
  • 4
  • 13

2 Answers2

8

You may have to tinker this around a bit but that will be something like that :

$nb_elem_per_page = 10;
$page = isset($_GET['page'])?intval($_GET['page']-1):0;
$data = (array)$this->view['data']['produkte'];
$number_of_pages = intval(count($data)/$nb_elem_per_page)+1;


<?php foreach (array_slice($data, $page*$nb_elem_per_page, $nb_elem_per_page) as $p) { ?>
...some html code here
<?php} ?>

<ul id='paginator'>
<?php
for($i=1;$i<$number_of_pages;$i++){?>
    <li><a href='./?page=<?=$i?>'>$i</a></li>
<?php}?>
</ul>
Loïc
  • 11,804
  • 1
  • 31
  • 49
  • 1
    Thanks a lot. I am almost there. The problem is that the basic url already uses a a query string like `www.domain.com/productsgroups?show=12` (where 12 is the number of a productgroup). I tried to modify your code like this: `
  • ` ($queryString contains the url including the query string). This gives me an url like `www.domain.com/productsgroups?show=12&page=1`. However, this does not work. I guess, I have to change `$page = isset($_GET['page'])?intval($_GET['page']-1):0;` – user2516117 Mar 03 '14 at 12:37
  • I don't think so. Is it normal that this : `www.domain.com/productsgroups?` has no .php extension? – Loïc Mar 03 '14 at 14:30
  • Yes, this is the standard behaviour. It's a WordPress site. The page does not have an extension. The plugins simply adds the query string ?show=xx to www.domain.com/page/. (The number references to a productgroup with the id=12.) Your code works fine and creates the correct pagination. The only problem is the link to the subpages that does not work. I have no idea, how to combine both query strings. – user2516117 Mar 03 '14 at 16:06
  • Then that will probably be ` – Loïc Mar 03 '14 at 22:19
  • I've just found out that the pagination is not working properly. With items per page set to, say, 3 the counter does show only the first page for 4, 5 and 6 items. With 7 items in the database, the pagination shows 2 x 3 items, but not item 7. It doesn't show item 7, 8, 9. For 10 itmes it shows 3 pages (9 items), but not item 10 and so forth. It seems that it only works with multiples of the number of items per page + 1. Do you have any ideas? – user2516117 Mar 06 '14 at 16:58
  • 2
    That did the trick: `$number_of_pages = intval(count($data)/$nb_elem_per_page)+2;` – user2516117 Mar 06 '14 at 17:25
  • on the pagination the for loop should be like less then or equal. here it is for($i=1;$i<=$number_of_pages;$i++){ – Shuhad zaman Oct 02 '18 at 10:17
  • @user2516117 I found a way in link of subpages that doesn't work. `
  • = $i ?>
  • ` – Mrvs Apr 12 '21 at 01:28