Jump to content

Excluding a page and rendering pagination


cjx2240
 Share

Recommended Posts

Hi, I'm struggling with what seems like it should be very straightforward.

I want to feature the first item of my blog in a bigger more detailed template, then list ten more items and pagination. How do I exclude my first item from the results list, but still loop through and paginate through ten other results?

Sounds dead simple right? I have tried several "obvious" methods to exclude the first item from the selector, which works fine, but the issue happens when you go to page two, it does not start the next page in the right place or it does not retrieve the right number of items.  

Things I have tried:

// exclude the first item by id
$newsItems = $pages->find("template=news-article, id!=$featuredId, limit=10");

// exclude with a slice
$newsItems = $pages->find("template=news-article, limit=10")->slice(1);

// start after item 1 (this just makes all pages start at item 1)
$newsItems = $pages->find("template=news-article, start=1, limit=10");

// keep the item but don't include it in my template, this leads to the wrong count of items on page 2
foreach($newsItems as $item) {
	if($item != $pages->find("template=news-article")->first()) {
		//etc
	}
}

As an alternative solution, is it possible to have a different count on a different page? Then I can simply include the featured item in the loop but style it differently. Again I have tried the obvious (change the count of the selector if $input->pageNum > 2) but it doesn't work

Link to comment
Share on other sites

I figured this out in the end, if anyone ever needs the answer in the future, I had to combine the first and last ideas, plus also exclude the first item from each page, and set the count one higher. There might be a better way but this is what I'm going with:

if($input->pageNum < 2) {
                       
  $featured   = $pages->find("template=news-article")->first;
  $featuredId = $pinned->id;
  
  // template for first item goes here
  
} else {
  
  $featuredId = 0;
  
}
  
$newsItems = $pages->find("template=news-article, id!=$featuredId, limit=11"); // 1 more than you want so you can exclude it in the template

foreach($newsItems as $item) {
  
	if($item->id != $featuredId && $item != $newsItems->first()) {
  
		// template for all other items
  
	}
  
}

 

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

  • Recently Browsing   0 members

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