2

I asked this question in the October forums but 12 hours later the post still says "unapproved" and I don't believe it's visible. Here's a copy-paste:

What I want to do is grab the latest blog post and display it on the homepage of the website. I can do it currently with my own post partial and the posts variable injected by the blogPosts component like so:

[blogPosts]
pageNumber = "{{ :page }}"
postsPerPage = 10
noPostsMessage = "No posts found"
sortOrder = "published_at desc"
categoryPage = "blog/category"
postPage = "blog/post"
==
{% partial 'site/blogpost' post=posts|last %}

However, I'd like to do this with the default blogpost component that comes with the plugin, but the only way to pass the post to the component seems to be by using the slug in the url, which doesn't really work for the homepage. How can I achieve this?

Firearrow5235
  • 196
  • 2
  • 13

2 Answers2

1

It is possible to use blogPost component but fetching last post slug and pass to it seems not good practice

what you do is you can use [blogPosts] component and set proper setting there to get latest/last blog

to make it possible

enter image description here

Posts per page : 1 [ as we need only last latest post ]

Post Order : Published(desc) [ you can change as you need ]

now just use proper markup to render it use default or just override to customise it.

Note : It will return list of post but in list there will be only 1 post as demanded so for custom markup just take care of that.

if any doubt please comment.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
  • This is good to know. I was thinking last night after implementing my solution that what happens to performance when I have 100 blog posts? I'd be pulling the whole database just for one post with my current implementation. – Firearrow5235 Mar 04 '19 at 19:23
  • So I just tried this, and using the blogPosts component it still pulls the entire list of blogs. What happens when you set "Posts per page" to "1" is that it paginates the list, placing only one listing on each page. So on the front page with the component I have the blog post, and then a list of links to the next pages (1, 2, 3, 4) which I don't want. – Firearrow5235 Mar 04 '19 at 20:13
  • larval pagination uses `limit` so it will only fetch given records in your case single record, but additional query fired for record count, you need to use that component so this was the easiest way, if have 100 post it will fetch only 1 of them and another query for total count, to remove link you need to expand and use markup in page [ override it ] – Hardik Satasiya Mar 06 '19 at 08:07
0

So, as with most things when it comes to development... RTFM.

All the info I needed was in the Components section of the October CMS docs, and of course I was only looking in the plugin docs. What I ended up doing was overriding the default component partial with my own and then passing the slug to the component. My original reason for wanting to use the default partial was that my custom partial wasn't rendering images, but the default would. I just needed to steal the line <p>{{ post.content_html|raw }}</p> to get that to work.

[blogPost]
==
...
{% component 'blogPost' slug=posts|last.slug %}

Additional info: With the above solution your template pulls all blog posts in the database, which means if you have a lot of posts this could (and most likely will) affect performance. Turns out October templates have a PHP section. The proper thing to do is to remove the blogPosts component and grab the latest post Model like so:

[blogPost]
==
<?
  use RainLab\Blog\Models\Post;

  function onStart()
  {
    $this['latestPost'] = Post::latest()->first();
  }
?>
==
{% component 'blogPost' slug=latestPost.slug %} 

Note: The blogPost component injects a post variable which will override a post variable defined in the PHP section. This is way the variable is labeled latestPost.

Firearrow5235
  • 196
  • 2
  • 13