Forum

Thread tagged as: Question, Problem, Blog

Latest/Most Recent post

Hi,

I'm looking to find out if anyone knows of a better way to achieve the following -

I want to make the latest (most recent) post stand out (styled differently) from the main blog listing on the blog page of a site.

Currently I've set this up by including a checkbox within the post template, so that a post can be marked "featured". On the blog list page, I'm then using "'match'=>'eq'" and "'match'=>'neq'" to pull the posts into the list/s:

Featured post list (one item only) -

<?php
  perch_blog_custom(array(
    'filter'=>'featured_post',
    'match'=>'eq',
    'value'=>'featured',
    'sort'=>'postDateTime',
    'sort-order'=>'DESC',
    'template'=>'blog/feature_post.html',
    'count'=>1,
  ));
?>

Remaining posts list -

<?php
  perch_blog_custom(array(
    'filter'=>'featured_post',
    'match'=>'neq',
    'value'=>'featured',
    'sort'=>'postDateTime',
    'sort-order'=>'DESC',
    'template'=>'post_in_list.html',
    'count'=>10,
    'paginate'=>true,
    'page-links'=>true,
  ));
?>

This works, but requires the user to update the featured checkbox each time a new post is added so that previously featured post is unchecked and the new post checked as featured.

Is there an easier option for doing this without a checkbox that I'm completely missing? I only ever need the Latest post, so not necessarily a featured post from the distant past. I also need paging so can't use a start count to display the remaining post list like below -

Featured post list (one item only) -

<?php
  perch_blog_custom(array(
    'sort'=>'postDateTime',
    'sort-order'=>'DESC',
    'template'=>'blog/feature_post.html',
    'count'=>1,
  ));
?>

Remaining posts list -

<?php
  perch_blog_custom(array(
    'sort'=>'postDateTime',
    'sort-order'=>'DESC',
    'template'=>'post_in_list.html',
    'count'=>10,
    'start'=>2,
    'paginate'=>true,
    'page-links'=>true,
  ));
?>

Thanks!

Mark Heggan

Mark Heggan 1 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you're setting the start value, you need to turn off the pagination - you can't both control the paging yourself and have Perch do it - it's one or the other.

A simpler route would be to branch in the template. If it's page 1 and the first item, then do this, else do that.

Thanks for the quick reply Drew.

Is there an example of how a perch "If" statement would detect "If it's the first page and first item"?

<perch:if #attribute#="#page-1# AND #first-item#">

    …latest post template code

<perch:else />

    …regular post template code

</perch:if>

What would I need to use for #attribute# / #page-1# / #first-item# values?

Thanks again for the help!

Drew McLellan

Drew McLellan 2638 points
Perch Support

I think you might be getting template languages mixed up. You should be able to do something like:

<perch:if id="current_page" value="1">
    <perch:if exists="perch_item_first">
        <perch:template path="featured_post.html" />
    <perch:else />
        <perch:template path="normal_post.html" />
    </perch:if>
<perch:else />
    <perch:template path="normal_post.html" />
</perch:if>

Cheers Drew, this makes much more sense now.

I just wasn't sure what attribute values I needed to include within the "If statement" to detect the "page number" and "first item"

Apologies for my dodgy #attribute# additions, couldn't think of a better way to explain which attribute/values that I wanted!

I will test this out, thanks again!