Homepage
Main navigation
Main content
Additional information
October Tricks
Login / Sign up
Quality Guidelines
About
GitHub
Propose new content
October CMS resources and help articles
Simple and to the point. Optimized by the community.
×
Login / Sign up
Quality Guidelines
About
GitHub
Propose new content
Edit trick
Changes will be published after manual review
Title
Give your trick a describing title. Do
not
start with «How to...».
Your trick
Keep it short and concise! Markdown is supported.
Sometimes, when creating hero sliders or similar components, it would be pretty handy to exclude blog posts which do not have any featured image attached to it. Well, thanks to Laravel this is quite simple with the following query, which is also supported in Laravel 6 (used on OctoberCMS v2): ```php $query = \RainLab\Blog\Models\Post::with(['categories', 'featured_images']) ->withCount('featured_images') ->where('featured_images_count', '>', 0); $posts = $query->listFrontEnd([ 'page' => 1, 'sort' => 'published_at desc', 'perPage' => 10, 'published' => true, }); ``` ## Using on your template The result stored in `$posts` can be added to your template layout or CMS page (either on the `onInit` or on the `onStart` function). However, you may will notice, that the post links / URLs do not exist, because we need to set them on our own. We're using the following solution for this: ```php ## url = "/test" description = "Example Template" == <?php function onStart() { $query = \RainLab\Blog\Models\Post::with(['categories', 'featured_images']) ->withCount('featured_images') ->where('featured_images_count', '>', 0)); // Select posts $posts = $query->listFrontEnd([ 'page' => 1, 'sort' => 'published_at desc', 'perPage' => 10, 'search' => null, 'category' => null, 'published' => true, ]); // Get component details if (!empty($component = $this->layout->getComponent('blogPosts'))) { $postPage = $component->property('postPage'); $categoryPage = $component->property('categoryPage'); } $ctrl = $this->controller; $postPage = empty($postPage) ? 'blog/post' : $postPage; $categoryPage = empty($categoryPage) ? 'category/post' : $categoryPage; // Set on each post and category $posts->each(function($post) use ($ctrl, $postPage, $categoryPage) { $post->setUrl($postPage, $ctrl); $post->categories->each(fn ($item) => $item->setUrl($categoryPage, $ctrl)); }); // Pass to template $this['featuredPosts'] = $posts; } ?> == <!-- Output --> ``` ## Bonus: Extend with BlogHub We extended this query for our upcoming Falcon template, on which the user is able to set either a `featured_image` or alternatively a custom `slider_image` meta value, implemented using our [BlogHub](https://octobercms.com/plugin/ratmd-bloghub) OctoberCMS extension. To ensure, that either at least the `featured_image` or the `slider_image` value is set, we extended the query as follows: ```php $query = \RainLab\Blog\Models\Post::with(['categories', 'featured_images', 'ratmd_bloghub_meta']) ->withCount('featured_images') ->where(function ($builder) { $builder->where('featured_images_count', '>', 0); $builder->orWhere(function ($builder) { $builder->whereHas('ratmd_bloghub_meta', function ($builder) { $builder->where('ratmd_bloghub_meta.name', '=', 'slider_image'); $builder->where('ratmd_bloghub_meta.value', '!=', null); }); }); }); ``` To include the `slider_image` field to your blog posts, just install our BlogHub extension and add the following code on the bottom of your current `theme.yaml` file: ```yaml ratmd.bloghub: post: slider_image: tab: Custom Meta type: mediafinder mode: image label: A special image used for sliders only. maxItems: 1 ``` The featured image, stored in the `slider_image` meta field, can then be output as follows: ```html <img src="{{ post.bloghub.meta.slider_image | media }}" alt="..." /> ```
References
Add additional online resources to your trick
×
Name
URL
×
Name
URL
+ Add reference
Topics
If your trick fits more than one topic select multiple. Select at least one.
Backend
Plugin Development
CMS
Twig
Themes
Deployment
Tags
You can use existing tags or create new ones. Add at least one.
Submit for review
Cancel
We use cookies to measure the performance of this website. Do you want to accept these cookies?
Accept
Decline