Jump to content

pagination on blog page


Alex
 Share

Recommended Posts

Hi,

I am including pagination on my blog page, its not quite working.

I have 5 test posts in total, showing the first 3 posts on the main blog page.

You can navigate to page 2 for the remaining 2 posts which works ok, but i'm losing my 'info' and 'about' pages in the main navigation which are children of the home page.

Also the pagination links are showing 6 pages, which are empty after page 2. (too many pagination links)

Here is the link to the blog page with pagination links at the bottom:

http://www.hoof.net.au/pwire/blog/

This is the pagination code in on my blog page template:

<?php
   $results = $pages->find("id>1, limit=10, sort=date");
   $pagination = $results->renderPager();
   echo $pagination;
   ?>

I tried a few things after reading the pagination info page which mostly resulted in no pagination links at all.

regards,

Alex

Link to comment
Share on other sites

Your selector "id>1, limit=10, sort=date" chooses whatever pages but not homepage. You probably want to add ie. template filter there, something like this: "id>1, limit=10, template=blog-post, sort=date".

That selector assumes that you have template called blog-post.

Link to comment
Share on other sites

I actually tried that one after reading a little about selectors, but my pagination has disappeared:

http://www.hoof.net.au/pwire/blog/

I tried 2 alternatives: 'blog' (main blog page template) and 'blog-entry' (where the blog post has its own page)

neither worked

So at the moment my code is this:

   <?php
   $results = $pages->find("id>1, limit=10, template=blog-entry, sort=date");
   $pagination = $results->renderPager();
   echo $pagination;
   ?>

I've allowed page numbers on the templates also.

Link to comment
Share on other sites

What code you use to output those blog previews? You should use same PageArray there so your pagination would match the actual content.

Something like this:

<?php
$blogEntries = $page->find("limit=10, template=blog-entry, sort=date");
foreach ($blogEntries as $entry) {
 echo $entry->title;
 echo "<br />";
}
$pagination = $blogEntries->renderPager();
echo $pagination;
Link to comment
Share on other sites

Okay its starting to make more sense to me now.

To output those blog previews I did this:

<?php
echo "<ul>";
$posts = $page->children("limit=3, sort=-date");
foreach($posts as $post) {
foreach($post->image as $image) {
$thumbnail = $image->size(460,0);
echo "<li><a class='workImg' href='{$post->url}'><img class='photo' src='{$thumbnail->url}' alt='{$image->description}' /></a>";
}
   echo "
    <h2 class='blogTitle'><a href='{$post->url}'>{$post->title}</a></h2>
    ";

   echo "
	 {$post->summary}
	 <a class='readMore' href='{$post->url}'><strong>read more...</strong></a> <br />
	 <div class='vertLine'><p><strong>Date: </strong> {$post->date}</p></div>
	 ";	  
echo "<div class='vertLine2'>";
echo "<span><strong>Category: </strong></span>";
if ($post->categories){ 
 foreach ( $post->categories as $category ) {
	 echo '<a href="' . $category->url . '">' . $category->title . '   </a>';

 }
echo "</div>";
echo "<div class='dottyBlog'></div>";
}
}
echo "</li>";
echo "</ul>";
?>

So i have amended my pagination code to be like your example:

   <?php
$posts = $page->find("limit=10, template=blog-entry, sort=date");
foreach ($posts as $entry) {
 echo $entry->title;
 echo "<br />";
}
$pagination = $posts->renderPager();
echo $pagination;

It gives me a list of all the post titles, but no pagination links? I would prefer to just have the numbered links for the additional pages.

http://www.hoof.net.au/pwire/blog/

thanks for quick replys by the way

Link to comment
Share on other sites

Also worth noting that pagination won't display if the limit you set is greater than the number of articles you have.

Sounds obvious but I confused myself tend other day first by not setting a limit and then by setting it too high ;)

Link to comment
Share on other sites

Yes, Pete is right.

You have only 5 posts and you want to paginate them 10 per page. No pagination possible in that scenario. What I tried to explain earlier is that you should use the exact same PageArray to paginate than to output. So take your first code and add this below it (after the first foreach):

$pagination = $posts->renderPager();
echo $pagination;
Link to comment
Share on other sites

thanks Apeisa & Pete,

I see what you mean about using the same Page Array to match content, I had it sitting outside all that.

i've got the pagination working by positioning the renderPager code as below, my only problem is that when I click to page 2, my main navigation still loses the 'Blog' and 'Info' pages... what would be causing that?

<?php
echo "<ul>";
$posts = $page->children("limit=3, template=blog-entry, sort=-date");
foreach($posts as $post){
foreach($post->image as $image) {
$thumbnail = $image->size(460,0);
echo "<li><a class='workImg' href='{$post->url}'><img class='photo' src='{$thumbnail->url}' alt='{$image->description}' /></a>";
}
   echo "
    <h2 class='blogTitle'><a href='{$post->url}'>{$post->title}</a></h2>
    ";

   echo "
	 {$post->summary}
	 <a class='readMore' href='{$post->url}'><strong>read more...</strong></a> <br />
	 <div class='vertLine'><p><strong>Date: </strong> {$post->date}</p></div>
	 ";	  
echo "<div class='vertLine2'>";
echo "<span><strong>Category: </strong></span>";
if ($post->categories){ 
 foreach ( $post->categories as $category ) {
	 echo '<a href="' . $category->url . '">' . $category->title . '   </a>';

 }
echo "</div>";
echo "<div class='dottyBlog'></div>";
}
}
$pagination = $posts->renderPager();
echo $pagination;
echo "</li>";
echo "</ul>";
?>
Link to comment
Share on other sites

Here's the navigation code:

<?php
				    $root = $pages->get("/");
				    $children = $root->children("limit=5");
				    $children->prepend($root);
				    foreach($children as $child) {
					    echo "<li><a href='{$child->url}'>{$child->title}</a></li>";
				    }
				    ?>
Link to comment
Share on other sites

  • 1 month later...

btw, this post helped me out a lot guys, thanks for the hard work :)

noticed this tho in an example of mine...

I have a bunch of 'test' posts for a page (using a template) that i have and i'm using the pagination (i think) correctly, and pagination works splendidly... (incoming!) however!

... interestingly enough - when i selected page 11 (in this case), it show the numbers from 11, 12, 13, 14, 15, ... , 16 (do you see it? :)

the "..." shouldn't be there at all - OR the "15" shouldn't be there hence allowing for the ...'s to take its place, but CERTAINLY not both.

thanks for any help/fix.

Bill

Link to comment
Share on other sites

  • 4 weeks later...

I can't work out how to get pagination for showing batches of blog posts. I've tried adding pagination from the main page doc and some post examples but nothing happens, it's as if I had not added the pagination code.

The URL I am trying to add pagination for is /category/cooking where /category/ is a real PW page and cooking is a URL segment (the template has Allow Page Numbers and Allow URL Segments ticked.)

Without pagination I see all pages that have cooking in their category field, 12 of them.

<?php
$category = $sanitizer->selectorValue($input->urlSegment1);
$blogposts = $pages->get("/blog/")->children;
$categorized_posts = $pages->find("category.count>0, sort=title");
foreach($blogposts as $blogpost)
{
foreach($blogpost->category as $category_to_test)
{
	if($category_to_test->name == $category)
	{
		echo "<h1>{$blogpost->get("headline|title")}</h1>";
		echo $blogpost->get("body");
	}
}
}

I wonder if my problem stems from the examples always having a single object/array holding the pages and in my case I don't, $blogposts is a super-set and I use the inner loops and if(...) to find the ones I want?

Any advice would be much appreciated.

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...