We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Discussion

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>
Shane Lenzen

Shane Lenzen 18 points

  • 3 years ago
Drew McLellan

Drew McLellan 2637 points
Perch Support

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 with current_page and perch_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.

Drew McLellan

Drew McLellan 2637 points
Perch Support

Hmm, I see. In that case show 4 items but don't show the first item on the first page.