Jump to content
mcgarriers

Simple Pagination

Recommended Posts

Hi,

I have the following code in a template that lists all the child pages of that parent.

I'd like to include Pagination so it would only show the title of 2 child pages per 'page'.

I have completed the process here http://processwire.com/api/modules/markup-pager-nav/ however, I'm now a little unsure how to integrate this into my code below. I tried the example code in that page but to no avail.

Can someone explain or point me in the right direction?

Here is my current code:

<?php
    if($page->numChildren) {
        echo "<ul class='nav'>";
        foreach($page->children as $child) {
        ?>       
                <?php echo $child->title; ?> 
        <?php
        }
        echo "</ul>";        
    }
?>
 

Many thanks for any pointers :-)

  • Like 1

Share this post


Link to post
Share on other sites

Did you try using "limit=2" in your selector?

The following should work.

<?php
if($page->numChildren) {
	echo "<ul class='nav'>";
	foreach($page->children("limit=2")as $child) {
		echo "<li><a href='{$child->url}'>{$child->title}</a></li>"; 
	}
	echo "</ul>";        
}
  • Like 3

Share this post


Link to post
Share on other sites

Thanks for the reply.

I'll try this first thing tomorrow and report back.

Do I need any additional code or is it purely adding the limit?

Thank you for your assistance.

Share this post


Link to post
Share on other sites

There's an example on that page you linked...

Let's look, to have PW pagination module work you need to use some search $pages->find() ..children() ..siblings() with a limit selector as explained above.

But this alone isn't going to cut it. You'll also need to render the pager. This, you do by using the result you get from the find() and add a renderPager() to it.

So the above code is missing that. Let's add the missing part.

<?php
if($page->numChildren) {
    $result = $page->children("limit=2");

    // render the pager
    echo $result->renderPager();
    
    // render the children 
    echo "<ul class='nav'>";
    foreach($result as $child) {
        echo "<li><a href='{$child->url}'>{$child->title}</a></li>";
    }
    echo "</ul>";

    // render pager again? ok
    echo $result->renderPager();
}

Wasn't that easy? ;)

  • Like 4

Share this post


Link to post
Share on other sites

Aaaand you also need to enable Page Number setting on the template you use this code.

  • Like 1

Share this post


Link to post
Share on other sites

Thanks Soma for adding that. I assumed mcgarriers was doing that already and the only problem was getting the results :)

Share this post


Link to post
Share on other sites

Many thanks for the replies guys.

@Soma Thankyou for this. I have tried this code and although the limit works, there's no sign of any pagination being rendered :'(

I have 'Allowed Page Numbers' for this template in the Admin area & I also have the 'Pager' Module installed.

Share this post


Link to post
Share on other sites

My bad. All fixed now. My issue was I had set the limit = 1 for testing purposes. I increased it to 2 and it started working :-)

  • Like 2

Share this post


Link to post
Share on other sites

Is it possible to set limit=1 and use the pagination? I would like to show one news article per page and still use the pager function.

Share this post


Link to post
Share on other sites

Not yet. I is scheduled on the roadmap with the release of version 2.5+.

  • Like 1

Share this post


Link to post
Share on other sites

While I've not had to do it before, I think you can approximate a limit=1 (for pagination purposes) just by specifying a "start" value based on the page number:

$item = $pages->find("limit=2, start=" . ($input->pageNum-1))->first();

The resulting $item would be the one you'd output on this pagination. 

  • Like 1

Share this post


Link to post
Share on other sites

Hi everyone,

somehow current class wont work for me here is my code :

<?php
				$news = $pages->find("parent=1009, limit=2");
				$out ="";
				$options = array(
					'upscaling' => false,
					 'cropping' => "0,0"
				);
					foreach($news as $newsitem){
						$newsimg = $newsitem->images->first;
						$out .= "<li><a href='{$newsitem->url}'><div>";
						if ($newsimg) {
							$newsimg = $newsimg->size(313,177,$options)->url;
							$out .= "<img src='{$newsimg}' alt='' />";
						}else{
							$out .="<div class='noImg'></div>";
						}
						$out .= "<div class='name'>{$newsitem->title}</div>";
						$out .= "<div class='date'>".date("j.m.Y", $newsitem->created)."</div>";
						$out .= "</div></a></li> ";
					}
					$pagination .="<nav class='pagination'>";
					$pagination .= $news->renderPager(array(
								    'nextItemLabel' => "Next",
								    'previousItemLabel' => "Prev",
								    'currrentItemClass' => "active",
								    'listMarkup' => "<ul>{out}</ul>",
								    'itemMarkup' => "<li>{out}</li>",
								    'linkMarkup' => "<a href='{url}'>{out}</a>"
									));
					$pagination .= "</nav>";
				 ?>
			<?php echo $pagination; ?>
			<ul class="news-list">
				<?php echo $out; ?>
				 <li class="fill"></li>
				 <li class="fill"></li>
			</ul>

every thing else works without problem. Any sugestions ?

Share this post


Link to post
Share on other sites

There's a typo: currrentItemClass should be currentItemClass :)

Share this post


Link to post
Share on other sites

There's a typo: currrentItemClass should be currentItemClass :)

DOH :D

i have to stop working late.

Ty Wanze i will try it later on when im back at home.

Share this post


Link to post
Share on other sites

I changed it to

'currentItemClass' => "active",

and still got no result, also classes for next and prev are not workin.

it works with default classes withour array like this :
 

$pagination .= $news->renderPager();

Also when i got pagination

sort=-date
 

does not seams to work ;/

Share this post


Link to post
Share on other sites

Did you look at the generated markup? It works fine here. Of course you have also to adjust your css to match the class you've given, otherwise you'll see no effect on the webpage itself.

Share this post


Link to post
Share on other sites

@Wanze

I went with 

$pagination .= $news->renderPager();

cause it does not matter what the class name is.

Worst thing is that when u have pagination there is a problem with sort :

sort=-date

does not work

i manage to display newes posts on my page but with a little trick.

In admin pannel i changed sort options to date.

and then in my template i used 

sort=-sort

Tho i dunno if there is a bug in module or i have some error ;)

Cheers

Share this post


Link to post
Share on other sites

Just going to add a note for something I noticed.

When building your selector don't use the revers() sorting/filtering function as makes renderPager() output nothing.

So use sort=something in the selector instead. I went with sort=created to sort the pages by created date.

  • Like 1

Share this post


Link to post
Share on other sites

Just going to add a note for something I noticed.

When building your selector don't use the revers() sorting/filtering function as makes renderPager() output nothing.

So use sort=something in the selector instead. I went with sort=created to sort the pages by created date.

I stumbled on this bug too?

Had to remove all the ->filters and concatenate them into a big string because renderpager() wouldnt function properly...supa weird

//$results = $pages->find("template=profile,sort=$sortby, limit=6");
//get results
// if ($location) 
// 	$results->filter("pro_page_location.id='$location'");
// if ($practice_area) 
// 	$results->filter("pro_page_practice_area.id='$practice_area'");
// if ($language)
// 	$results->filter("pro_page_language='$language'");
// if ($position)
// 	$results->filter("pro_page_position='$position'");

if ($location) 
	$filters .= ",pro_page_location.id=$location";
if ($practice_area) 
	$filters .= ",pro_page_practice_area.id=$practice_area";
if ($language)
	$filters .= ",pro_page_language=$language";
if ($position)
	$filters .= ",pro_page_position=$position";

$results = $pages->find("template=profile,sort=$sortby, limit=6" . $filters);

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By brdje
      Greetings,
       
      I've created a product database which all use the template product.php and are published on the website.
      I'm trying to create an overview table with pagination of all the products, but using $pages->get does not return an object, only the object title.
      $products = $pages->get("template=product, limit=10"); This returns 10 strings in the frontend but not the object. When looping over the result set to get product fields, i get the following error:
      Notice: Trying to get property 'title' of non-object
       
      Because the product database is fairly large, i've added a pagination using the same query and this does return all the pages, but i can't click on the links that the paginator renders.
      $results = $pages->find("template=product, limit=10"); if($results->getTotal() > 10) { echo $results->renderPager(array( "nextItemLabel" => "Volgende", "previousItemLabel" => "Vorige", "currentItemClass" => 'active' )); }
      I've added the option in the template to allow page numbers. When i navigate to the link manually, i still end up seeing the 10 first products.
       
      I've tried changing several settings around, but i'm a bit stuck on how to resolve these issues. Do you have any advice what I need to adjust?
      Thanks in advance for your feedback.
       
       
    • By Peter Troeger
      Hello Community 🙂
      Has anyone ever tried having multiple elements on one page that get their info with $pages->find('selector, limit=n') and tried using pagination on one of these elements without effecting the other?
      I have a slider on a page where I display content with pagination. But when I go to page two, the slider content also goes to page two, which I don't want it to do 🙂
      Any tips are greatly appreciated!
      Thanks!
      -Peter
    • By Peter Troeger
      Hello 🙂
      I have set up pagination on a mulitlanguage site.
      I've done this before, but this time I have a problem I can't solve.
      Pagination is activated on 'parent-template' and 'child-template' just to be sure 🙂
      This is my code:
      $children = $page->children('limit=1'); foreach($children as $child) { $title = $child->title; echo $title; } echo $children->renderPager(); The navigation is output correctly and the link look correct as well 'parent-page/page2/'.
      But when I click the link, the site seems to redirect back to 'parent-page/
      Any help would be greatly appreciated 🙂
      - Peter
    • By MateThemes
      Hello everyone!
      I have a problem with pagination. I have following code:
      <div class="uk-section"> <?php $results = $pages->find("template=gallery, limit=10, sort=title"); $pagination = $results->renderPager(); echo $pagination; echo "<ul class='uk-pagination uk-flex-center'>"; foreach($results as $result) { echo "<li><a href='{$result->url}'>{$result->title}</a></li>"; } echo "</ul>"; echo $pagination; ?> </div> Which works fine. And this markup results in following html code:
      <div class="uk-section"> <ul class='uk-pagination uk-flex-center'> <li> <a href='/galerie-bisheriger-projekte/'>Galerie bisheriger Projekte</a> </li> <li> <a href='/galerie-bisheriger-projekte/galerie-1/'>galerie-1</a> </li> </ul> </div> But instead of "Galerie bisheriger Projekte" and "galerie-1" I want to have a number.
      And as last question, how can I add a previous and next button?
      Thanks for your help!
    • By Liam88
      Hi all,
      Quick question as I haven't found anything from my Googling. 
      I have a blog on the site which utilises pagination. On the standard blog page I have a custom header which has featured posts.
      Below that I then have recent posts like mosts blogs.
      Now if i click to page two or three I want the header to disappear and just show a continuation of the standard posts.
      I'm not sure how to go about this so any direction would be helpful.
      Thanks
×
×
  • Create New...