Jump to content

Pagination, clean urls, categories? Ohhh my...


Recommended Posts

Hello All, so after finding most of my information from scouring the forums. I was able to build a page that sorts it's children based on their category. This works out great. For this particular use case, I also need to page through each individual child, by use a next and prev button. The catch to this, is that I also want the url to updated.

the url should read: http://site.dev/ad-p...ategory/product

But instead it reads: http://site.dev/ad-p.../category/page2 when I add pagination to the mix.

So I'm pretty stuck now and I'm not sure how to proceed.

Also the way I went about using categories, if there is a simpler solution or best practice, please let me know. I would like to improve as much as I can.

Thanks and here is the code I'm using below:

 $name = $sanitizer->pageName($input->urlSegment1);
 $prodName = $sanitizer->pageName($input->urlSegment2);			
 $bodyContent = $pages->get("/ad-products/$prodName");

// get all categories
 $categories = $pages->get("/categories/")->children;
 $products = $pages->get("/ad-products/");

// loop categories
 foreach($categories as $cat) {
//class is on if on the page($cat) or the child(what's the parent)
$class = $name == $cat->name ? " class='on'" : '';
echo "<li $class><a href='{$products->url}{$cat->name}/'>{$cat->title}</a></li>";

<div id="content">
 // output pagination links
 $prod = $page->children("limit=2, sort=-date");
 $pagination = $prod->renderPager();
 echo $pagination;

 echo "<p>$bodyContent->body</p>"

 //3rd Nav
 echo  "<nav id='sec-nav'>".
	"<li><em>Skip To:</em></li>";

 foreach($categories as $cat) {
$prodName = $sanitizer->pageName($input->urlSegment2);
// find all samples having the current category
$products = $pages->get("/ad-products/")->find("categories=$cat");

if( $name == $cat->name ) {
  foreach($products as $child) {
 $class = $prodName == $child->name ? " class='on'" : '';
 echo "<li $class><a href='{$child->name}'>{$child->title}</a></li>";
 echo  "</ul>".

</div><!-- /content -->
Link to comment
Share on other sites

Since you are wanting to do next/prev with single products, I don't think you'll want to use PW's pagination at all. I think this also makes sense given that you want the URL to reflect the product name rather than a page number. So if I'm understand your example correctly, I think you'll want to do something like this:


$cat = the current category page; 
$product = the current product page;
$products = $pages->find("parent=/ad-products/, categories=$cat");

$prev = $product->prev($products);
$next = $product->next($products);

if($prev->id) echo "<a href='./{$prev->name}'>Previous: {$prev->title}</a> ";
if($next->id) echo "<a href='./{$next->name}'>Next: {$next->title}</a>";
Link to comment
Share on other sites

Thank you sir!!! That worked a charm!!! I'm amazed at the simplicity of it. :)

I know what you mean - many times I've looked for a complicated solution where a simpler one would do in ProcessWire.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...