Forum
Blog Posts in List - Paginate and Start With Second Post
I'm building a site in which the designer shows a hero blog post on the first page of the blog post list. After realizing that start
cannot be used with 'paginate'=>true
in perch_blog_custom
, I rolled my own solution. Just wanted to drop this here in case it helps someone else, as I saw some other Perchers in the forum with similar situations.
// get current page
if (perch_get('page')) {
$curpage = perch_get('page');
} else {
$curpage = 1;
}
// hero post - shows on first page only
if ($curpage == 1) {
perch_blog_custom([
'count'=>1,
'sort'=>'postDateTime',
'sort-order'=>'DESC',
'template'=>'hero_post.html',
'start'=>1,
'paginate'=>false,
]);
}
// posts in list, starting with post 2
$count = 4;
$start = ((($curpage - 1) * $count) + 2);
perch_blog_custom([
'count'=>$count,
'sort'=>'postDateTime',
'sort-order'=>'DESC',
'template'=>'post_in_list.html',
'start'=>$start,
'paginate'=>false,
]);
// begin paging div
?> <div class="paging"> <?php
// check if previous page exists
$prev = perch_blog_custom([
'count'=>1,
'sort'=>'postDateTime',
'sort-order'=>'DESC',
'start'=>((($curpage - 2) * $count) + 2),
'paginate'=>false,
'skip-template' => true,
]);
if (!empty(array_filter($prev))) { ?> <a href="/news?page=<?php echo ($curpage - 1); ?>">Previous</a> <?php }
// check if next page exists
$next = perch_blog_custom([
'count'=>1,
'sort'=>'postDateTime',
'sort-order'=>'DESC',
'start'=>(($curpage * $count) + 2),
'paginate'=>false,
'skip-template' => true,
]);
if (!empty(array_filter($next))) { ?> <a href="/news?page=<?php echo ($curpage + 1); ?>">Next</a> <?php }
// end paging div
?> </div>
Could you not include the hero template inside the main listing? Then you could get it all with one call.
Well, I tried a combination of
perch:if
withcurrent_page
andperch_item_index
using'paginate'=>true
, but I couldn't find a way to show the hero post + 4 posts in list on the first page only, and then 4 posts in list on every page after that.Hmm, I see. In that case show 4 items but don't show the first item on the first page.