Useful links
Transcript
The default pages and templates will give you a working blog. However it may be that you want to do something a bit different with your blog posts. Perhaps display the latest 2 on the homepage of your site.
To do this you use perch_blog_custom() a function that accepts an array of options to select exactly what you want, and pass in your own template for the output.
In my example site I have space for two news items. So I want to display the latest 2 items from the blog, and I only want to display the items in the news category.
First I am going to make my own template to display the items. Taking post_in_list.html as a starting point, so I copy post_in_list and rename it to homepage_post_in_list.html.
I can now edit this template to use the markup from my homepage. It is actually a bit simpler than post_in_list.html so I’m removing elements here.
With my template ready I just need to get Perch to display the blog posts that I’m interested in. So, on my homepage I replace the news content with the function
<?php perch_blog_custom($opts); ?>The value that I pass in – $opts – is an array of options. This is the same as using perch_content_custom and is also the same as our other apps. If you need to pass in options you need to create an array.
Our array needs to order the items by postDateTime descending – so the newest item will be first. Then only select 2 items. We also need to pass in the news category and the new template.
The completed section of PHP on your page looks like:
<?php
$opts = array(
'sort'=>'postDateTime',
'sort-order'=>'DESC',
'category'=>array('news'),
'template'=>'blog/homepage_post_in_list.html'
);
perch_blog_custom($opts); ?>Save the homepage, reload and your latest 2 items in the news category will display. This will automatically update as you add new items to your blog.
It is worth looking at the different options for perch_blog_custom as you can do all kinds of things – for example ordering the items randomly, filtering between two dates and so on.
