Hi guys, I’m jumping on the Sage train after some years of not doing anything related with developing and I feel like I know nothing about modern tools.
I’ve read so far Blade docs, soberwp/controller docs and numerous posts here related to Controller itself but it’s not helping me a lot. Docs are too general and even when I understand examples there, I cannot make something by myself.
Maybe you will help me, as I tend to learn more by dissecting real life examples.
So. I want to make sticky post (featured) as separate item to be able to place it on top of my page (blog). The best way would be to first hide it from archive and then use it outside of main loop.
I’ve found this, And I have honestly no idea how to make it in “Controller way”:
Instead of closing your PHP tag to print out some HTML, return an array containing the data you want to be used, and then create a blade that uses that data.
@alwaysblank I’ve edited code in my question as there were some leftovers from copy/paste.
This code is working fine. But I’m asking how to “translate” it for the Controller. Or maybe there is another way how to modify loop to get same result? Sorry for my not understanding general statements. Please treat me as utter idiot
So if I’m understanding correctly, you want to completely separate your normal posts from your sticky posts, and you want to keep the “sticky query” in a controller.
app/Controllers/App.php (or other appropriate controller):
...
// This is untested but it should be close to working
public function stickies() {
// Initialize the return array
$return = array();
// Get the sticky posts
$sticky = get_option( 'sticky_posts' );
// Set up arguments for sticky posts
$args = array(
'posts_per_page' => -1,
'post__in' => $sticky,
'ignore_sticky_posts' => 1
);
// Get all sticky posts as an array
$return = get_posts( $args );
// Always return
return $return;
}
...
And then in your template…
@foreach($stickies as $sticky)
@include('partials.sticky', $sticky)
@endforeach
There is missing @ before include and ’ ’ around partials.sticky but it’s working great! Thank you for this solution.
Now, can you tell me how performance wise this is different from the method I’ve come up to in my Edit 2?
It’s for sure more flexible as I don’t need to run whole loop in other place then home, right?
Edit:
There is something wrong. I’ve glued 3 posts. No. 6, 7, 8. My method is displaying them correctly but yours is sticking only last one 3 times. I’ve changed array args as below:
public static function stickies() {
// Initialize the return array
$return = [];
// Get the sticky posts
$sticky = get_option( 'sticky_posts' );
// Set up arguments for sticky posts
$args = [
'posts_per_page' => 3,
'post__in' => $sticky,
];
// Get all sticky posts as an array
$return = get_posts( $args );
// Always return
return $return;
}
@foreach(App::stickies() as $sticky)
<h3>{{ $sticky->post_title }}</h3>
@endforeach
I’ve placed var_dump($sticky) before @endforeach and the result is correct but {!! get_title() !!} or {{ post_class() }} are still the same, which is last sticky post. Here is example:
Post number 8 // < this is last sticky post title assigned to what is below {!! get_title() !!}
object(WP_Post)#5374 (24) {
["ID"]=> int(23)
["post_author"]=> string(1) "1"
["post_date"]=> string(19) "2019-04-15 16:01:46"
["post_date_gmt"]=> string(19) "2019-04-15 14:01:46"
["post_content"]=> string(2568) "
// ... content here
" ["post_title"]=> string(12) "Post number 6"
["post_excerpt"]=> string(0) ""
["post_status"]=> string(7) "publish"
["comment_status"]=> string(4) "open"
["ping_status"]=> string(4) "open"
["post_password"]=> string(0) ""
["post_name"]=> string(12) "post-number-6"
["to_ping"]=> string(0) "" ["pinged"]=> string(0) ""
["post_modified"]=> string(19) "2019-04-15 23:26:03"
["post_modified_gmt"]=> string(19) "2019-04-15 21:26:03"
["post_content_filtered"]=> string(0) ""
["post_parent"]=> int(0)
["guid"]=> string(32) "//localhost:3000/wordpress/?p=23"
["menu_order"]=> int(0)
["post_type"]=> string(4) "post"
["post_mime_type"]=> string(0) ""
["comment_count"]=> string(1) "0"
["filter"]=> string(3) "raw" } ;