Jump to content

Simple Pagination


mcgarriers
 Share

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
Link to comment
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
Link to comment
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
Link to comment
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.

Link to comment
Share on other sites

  • 2 months later...

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
Link to comment
Share on other sites

  • 1 month later...

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 ?

Link to comment
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 ;/

Link to comment
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

Link to comment
Share on other sites

  • 1 year later...

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
Link to comment
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);
Link to comment
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
 Share

×
×
  • Create New...