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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By picarica
      Hello si have this gallery, pretty good website, but yesterday client uploaded over 3000 images oto the site, and it cannot handle filtering them out and showing them at the same time , i wanted some simple pagination maybe infinite scrool or onclikc load whatever, but i cannot seem to implement infinite ajax scrool and any other JS methods, nut sure why i still got stuck at the next method, like there is not next page.
      so i wanted to implement PW method of paginating i wanted to use MarkupPagerNav
      so far i have  this code for outputting images
      $pa = $pages->find("template=basic-page|art_gallery, images.tags!=''"); /* $pa = $pages->find("has_parent!=2,id!=2|7,status<".Page::statusTrash.",include=all"); */ echo "<div class='js-filter' id='gal' >"; /* row gtr-50 gtr-uniform */ $frame = $pages->get('/settings/')->watermark; foreach ($pa as $p) { foreach($p->images as $image) { if(!$image->hasTag("act")) { $options = array('quality' => 80, 'upscaling' => true, 'cropping' => 'north', 'sharpening'=>'medium'); $large = $image->size(1200, 0, $options); $wmImage = $large->pim2Load('wm1', ['quality'=>80, 'upscaling' => true, 'sharpening'=>'medium', 'defaultGamma'=>-1])->watermarkLogo($frame, $position='se',$padding=1.5)->pimSave(); /* zmazanie variacii, treba odpoznakovat ked sa menia nastavenie vyssie */ /* $image->pim2Load('wm1')->removePimVariations(); */ $thumb = $image->size(400, 300, $options); echo "<div class='$image->tags grid-item' style=''>"; echo "<span style='overflow:hidden;'class='image fit'>"; echo "<a class='hvr-reveal' href='$wmImage->url'>"; echo "<img uk-scrollspy='cls: uk-animation-fade; repeat: false' src='$thumb->url' alt='$image->tags'>"; echo "</a>"; echo "</span>"; echo "</div>"; } else { $options = array('quality' => 80, 'upscaling' => true, 'cropping' => 'north', 'sharpening'=>'medium'); $large = $image->size(1200, 0, $options); $wmImage = $large->pim2Load('wm2', ['quality'=>80, 'upscaling' => true, 'sharpening'=>'medium', 'defaultGamma'=>-1])->pixelate(25)->smooth(255)->watermarkLogo($frame, $position='se',$padding=1.5)->pimSave(); /* zmazanie variacii, treba odpoznakovat ked sa menia nastavenie vyssie */ /* $image->pim2Load('wm2')->removePimVariations(); */ $thumb = $image->size(400, 300, $options); echo "<div class='$image->tags blur grid-item' style=''>"; echo "<span style='overflow:hidden;'class='image fit'>"; echo "<a class='hvr-reveal' href='$wmImage->url'>"; echo "<img uk-scrollspy='cls: uk-animation-fade; repeat: false' src='$thumb->url' alt='$image->tags'><span>18+</span>"; /* <span>BY OPENING THIS IMAGE YOU CONSET THAT YOU'RE 18 YEARS OR OLDER</span> text copyraightova,y dat vedla obrazky potom */ echo "</a>"; echo "</span>"; echo "</div>"; } } }; to put it  simply, it first searches for sites, and then outputs all images from those sites, pretty simple, it also uses watermarking and uikit filtering
      i have filtering done like this
      $num = 1; echo "<ul class='uk-subnav uk-subnav-pill'>"; foreach(array_unique($alltags) as $key => $tag) { echo"<li uk-filter-control='filter: .$tag;group: $num' class='butt$num' uk-toggle='target: .butt$num ; animation: uk-animation-fade; queued: true'><a href='#'>$tag</a></li>"; echo"<li uk-filter-control='group: $num' class='butt$num active' aria-hidden='true' hidden='' uk-toggle='target: .butt$num; animation: uk-animation-fade'><a href='#'>remove - $tag</a></li>"; $num++; } echo "</ul>"; simple, but i have NO idea how to implement pagination, just because it seems to work that it like find all images, and stores with limit, and then just paginates them, but i cannot apply this method in my code.
      any idea how to make any JS inifite scroll work ? or just how to make this work ? with my setup, or with some modifications, it just have to work as is now
    • By creativejay
      I will preface this by saying I have checked (multiple times) per template (both parent and child, to be totally sure) that they are set to allow pagination.
      What's happening is that my first page of results keeps displaying, despite /page2/ being in the URL. The pagination markup also indicates I am still on page one. This is happening across multiple types of paginated pages.
      $pagination = $pager->render($results, $poptions); The options are just markup...
      $poptions = array( 'numPageLinks' => 5, 'listClass' => 'uk-pagination', 'linkMarkup' => "<a href='{url}'>{out}</a>", 'currentItemClass' => 'uk-active', 'separatorItemLabel' => '<span>&hellip;</span>', 'separatorItemClass' => 'uk-disabled', 'currentLinkMarkup' => "<span>{out}</span>", 'nextItemLabel' => '<i class="uk-icon-angle-double-right"></i>', 'previousItemLabel' => '<i class="uk-icon-angle-double-left"></i>', 'nextItemClass' => '', // blank out classes irrelevant to Uikit 'previousItemClass' => '', 'lastItemClass' => '', ); In the header, I call for the module and the options include:
      $pager = $modules->get('MarkupPagerNav'); include_once("pagination.inc"); Aside from the usual "check that you allowed pagination" advice, what issue might these symptoms indicate?
    • By ottogal
      Hello all,
      using PW 3.0.148 with the regular site profile for a blog, I got an an empty pagination output when I had a Toggle field in the selector.
      The Toggle Fieldtype was introduced with https://processwire.com/blog/posts/pw-3.0.139/ .
      The selector resulting in empty pagination:
      $posts = $pages->find("parent=blog, sort=-date, limit=10, toggle_field=0"); It worked well, when I replaced the Toggle field with a Checkbox field:
      $posts = $pages->find("parent=blog, sort=-date, limit=10, checkbox_field=0"); So the prerequisites for the pagination to work are given.
      The settings for the Toggle field were:
      Formatted value: Integer Label Type: Yes/No Input Type: Toggle buttons Default selected option: No Thanks for any hints!
    • By jacmaes
      I have a series of videos, and the following search form (translated into English here) that allows to filter these videos on the frontend:

      I've built a few of these search forms, but only with text fields, selects and radio buttons. Here I'm using an array with checkboxes ("Level" field above), and it's causing me grief when I try to paginate these results. I've done a lot of searching in the forum and spent too many hours to try to get it to work. Here's how I'm building the selector:
      <?php if(count($input->get)): // Level is an array. Code adapted from Ryan's snippet here: // https://processwire.com/talk/topic/3472-enable-pagination-for-search-results/?tab=comments#comment-38042 if($input->get->level) { $level = array(); foreach($input->get->level as $id) $level[] = (int) $id; // sanitize to INTs $level = implode('|', $level); // convert to 123|456|789 string, ready for selector } else { $level = ''; } $data = array( 'training_type' => array('=', (int) $input->get->training_type), 'duration' => array('=', (int) $input->get->duration), 'level' => array('=', $level), 'limit' => array('=', (int) $input->get->limit) ); $selector = ''; // iterate through the $data we made above to create a selector string foreach($data as $field => $a) { list($operator, $value) = $a; if(empty($value)) continue; // send value to the whitelist so that it can be used in pagination $input->whitelist($field, $value); // append to our selector string $selector .= "$field$operator$value, "; } $videos = $page->children("$selector"); When I hit search, I get the expected results. So far so good. The GET parameters are the following with the options selected in the screenshot above:
      videos/?level[]=1476&level[]=1477&training_type=1473&duration=1485&limit=10 $selector echoes the following as the "level" field is an array with a pipe character:
      level=1476|1477, training_type=1473, duration=1485, limit=10 Now, when paginating these results, the following page (page 2) shows these GET parameters:
      videos/page2/?level=1476|1477&training_type=1473&duration=1486&limit=10 And I think that's where the problem lies. The "level" field is "lost" and I'm getting more results than expected on subsequent pages. If I manually add "page2" to the initial results in the URL, just to test, everything works fine:
      videos/page2/?level[]=1476&level[]=1477&training_type=1473&duration=1486&limit=10 But how can I achieve this in code? Do I need to revert to "level[]=1475&level[]=1477" instead of "level=1476|1477" for the pagination to work correctly, and can you PHP gods illuminate me?
      Any help would be really appreciated, really.
    • By christophengelmayer
      Hi everyone,
      I'm working on a CLI script that renders paginated pages.
      Therefore I iterate trough paginated pages and set the page number and render the result.
      My problem is, after calling the render function for the first time, the output doesn't change even if I change the page number.
       
      DEMO:
      I'm using a template that renders a pagination of its children:
      <?php echo $page->children("limit=3")->render(); ?> I can view the paginated results in the Browser:
      /page-rendering-pagination/ /page-rendering-pagination/page2 /page-rendering-pagination/page3 ... When trying to render the different pages using the API I always get the first result, even if I change the page Number.
      <?php namespace ProcessWire; include('./index.php'); wire('input')->setPageNum(1); $p = wire('pages')->get('/page-rendering-pagination'); var_dump($p->render()); // renders first three items wire('input')->setPageNum(2); var_dump($p->render()); // also renders the first three items Am I missing something? Is there some kind of caching mechanism that I'm not aware of?
      Thanks for your help.
×
×
  • Create New...