Forum

Thread tagged as: Question, Runway

How to show most recent content on one page from two different collections?

I'm new to Perch Runway. Building a website that has two collections of posts — blog and news. Each collection has published date. I want to display on homepage only content from collection that has most recent published date.

For example: If I publish today new post in blog collection, I need to show only that post on the homepage. If I post another article in a news collection tomorrow, I'd like to show only that article on the homepage.

Is it possible to do this kind of thing with Perch, and if yes, how? Thanks

Tomislav Jurisic

Tomislav Jurisic 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

perch_collection(['Blog', 'News'], [
'sort' => '_date', 
'sort-order' => 'DESC',
'count' => 1,
]);

Thanks, Drew. Now, I'm in this situation. Here is the code, but now my homepage displays 0 articles. I know that something is not right with my code, but I don't see what....? :/ The problem is that every section has its own template

    <?php
        perch_collection(['Blog', 'News'], [ 
            'sort' => '_date',  
            'sort-order' => 'DESC', 
            'count' => 1, 
        ]);

        perch_collection('Blog', [
            'template'   => 'blog.html',
            'sort'       => 'date',
            'sort-order' => 'DESC',
            'count'      => 1,
        ]);

        perch_collection('News', [
            'template'   => 'news.html',
            'sort'       => 'date',
            'sort-order' => 'DESC',
            'count'      => 1,
        ]);
    ?>

Probably the comma at the end of the last key in each perch_collection.

'count' => 1,

Should be

'count' => 1

Tried that, nope. But I got strange result now. First field is empty, then, I got listed all the articles from first category, and then from other. All in one long list...?

I just want to display one item on the homepage, the one that has most recent published date

Use this one alone

<?php
perch_collection(['Blog', 'News'], [
'sort' => '_date',
'sort-order' => 'DESC',
'count' => 1
]);
?>

I've just tried that but didn't get any result. May I ask how does Perch knows what template to use if for example "News" collection has most recent published date? I think that I should include info on what template to use with that collection?

If you put 'count' => 3 do you see any results? Here's the perch_collection documentation

https://docs.grabaperch.com/functions/collections/perch-collection/

Specify templates with

        'template'   => 'article.html'

If I put 'count' => 3, I get three results. First two are blank, and the third is ok. But all three are from one collection.

The thing is, I use different template for every collection. if I define only template from news collection, how can Perch "know" what template to use for Blog collection. I think I'm missing some "if" statement here......

Just tried to put 'count' => 10 and since I got only two (for now) articles, and the rest are all blank. But all of them are from one collection only

I think I've figured out part of the solution. Those blank fields are posts from other collection. And they are not displayed because they are trying to fit in the template defined by the first collection.

Is there a way to use code provided above (that apparently works), but to have some kind of a "if" statement (or something) that use the right template for that collection item.

For example - if collection item from the News category has the most recent published date, use template 1, if collection item from the Blog category has the most recent published date, use template 2........

Duncan Revell

Duncan Revell 78 points
Registered Developer

As you've worked out, if you provide an array of collections without specifying a template, it will use the template of the first collection in the array. Swap them round ([News, Blog]) to see if that proves the point.

Your best bet is probably to create another template that combines the two templates you already have and provide that as an option to your combined request. the template may need a fair few <perch: if exists=".."> to test if the ids are there, or you may end up with a lot of random gaps in your output.

Depending on your PHP skill/preference, you could also tell that request to skip template processing, check the result using PHP and then pass the result through the appropriate template.

Thanks Duncan! That's a good point. Was thinking to try with one template that combines two templates and test it with perch: if.

But I just think that more elegant solution would be to check the result using PHP. Will try to read more trough Perch documentation (https://docs.grabaperch.com/perch/content/functions/get-content-before-templating/) and other posts on forum and see if I can come up with a solution. Since my background is in design, I'm currently on w3schools.com website, learning "Creating (Declaring) PHP Variables" so I could combine example code found there with Perch documentation and see if I can make it work :))

Rachel Andrew

Rachel Andrew 394 points
Perch Support

If you are not sure which template Perch is using, or whether you are getting the data you expect then enable debug in Perch and it will tell you.

Thanks Rachel. The real problem is that I'm not a developer (don't know how to code) so I'm stuck on using, reusing and testing code that I found on forum.

Duncan Revell said:

Depending on your PHP skill/preference, you could also tell that request to skip template processing, check the result using PHP and then pass the result through the appropriate template.

I know that this is a solution for my current problem, but just don't know (yet!) how to translate it to code :)