Question

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!

Was it helpful?

Solution

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>

OTHER TIPS

You can use array_splice (twice) to get a new array with only the product you need. Say you want to show 10 items per page and start at page 3:

<?php
$all_products = (array)$this->view['data']['produkte'];
$all_products = array_splice($all_products, 20); //select where to start
$all_products = array_splice($all_products, 0, count($all_products) - 10); //select how many products to show
foreach ($all_products as $p) {
    //...some html code here
}
?>

(not tested)

Edit: @Loïc's answer is better as it only uses one array_splice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top