操纵 Timber $context['posts'] 对象中的 post 顺序
manipulating post order in Timber $context['posts'] object
我试图确保我的 {{ posts }} 对象中的前两个帖子有缩略图,而其余帖子继续按最近发布的顺序排序。我正在尝试创建这样的过滤器来完成此操作:
PHP中的过滤器定义:
function posts_with_thumbs_first_filter( $posts, $num_top_posts = 2 ){
// find first posts with thumbnails in them to decide what posts need to be reordered to first position in the array
$first_posts_with_thumbs = array();
foreach ($posts as $key => $post) {
if ($post->thumbnail) {
$first_posts_with_thumbs[] = $key;
if (count($first_posts_with_thumbs) == $num_top_posts) { break; }
}
}
...manipulate order of posts here...
return $posts;
}
在 Twig 中使用过滤器:
{% for post in posts|posts_with_thumbs_first %}
...regular post output here, only with posts now reordered...
{% endfor %}
但看起来操纵帖子的顺序很棘手,因为它不是标准数组,它是一个 Timber\PostQuery 对象,其中包含控制帖子数据的私有迭代器和方法。我不能使用 PHP 的数组函数,如 array_slice、array_splice 等,因为 posts 不是数组,它是一个对象。
是否有可靠的方法来操纵帖子的顺序?
这是一种方法:将帖子对象转换为数组以启用 PHP 的数组操作函数。
function posts_with_thumbs_first_filter( $posts, $num_top_posts = 2 ){
$posts = (array) $posts; // convert posts object to array to use PHP's array manipulating functions
$first_posts_with_thumbs = array();
foreach ($posts as $key => $post) {
if ($post->thumbnail) {
$first_posts_with_thumbs[] = $key;
if (count($first_posts_with_thumbs) === $num_top_posts) { break; }
}
}
$posts_to_move_to_top = array();
for ($i = count($first_posts_with_thumbs) - 1; $i >= 0; $i--){
$posts[$first_posts_with_thumbs[$i]]->moved = true;
$posts_to_move_to_top[] = array_splice($posts, ($first_posts_with_thumbs[$i]), 1); // extract post from posts array
}
foreach ($posts_to_move_to_top as $post) { // put back into posts at beginning
array_unshift($posts, $post[0]);
}
return $posts;
}
我试图确保我的 {{ posts }} 对象中的前两个帖子有缩略图,而其余帖子继续按最近发布的顺序排序。我正在尝试创建这样的过滤器来完成此操作:
PHP中的过滤器定义:
function posts_with_thumbs_first_filter( $posts, $num_top_posts = 2 ){
// find first posts with thumbnails in them to decide what posts need to be reordered to first position in the array
$first_posts_with_thumbs = array();
foreach ($posts as $key => $post) {
if ($post->thumbnail) {
$first_posts_with_thumbs[] = $key;
if (count($first_posts_with_thumbs) == $num_top_posts) { break; }
}
}
...manipulate order of posts here...
return $posts;
}
在 Twig 中使用过滤器:
{% for post in posts|posts_with_thumbs_first %}
...regular post output here, only with posts now reordered...
{% endfor %}
但看起来操纵帖子的顺序很棘手,因为它不是标准数组,它是一个 Timber\PostQuery 对象,其中包含控制帖子数据的私有迭代器和方法。我不能使用 PHP 的数组函数,如 array_slice、array_splice 等,因为 posts 不是数组,它是一个对象。
是否有可靠的方法来操纵帖子的顺序?
这是一种方法:将帖子对象转换为数组以启用 PHP 的数组操作函数。
function posts_with_thumbs_first_filter( $posts, $num_top_posts = 2 ){
$posts = (array) $posts; // convert posts object to array to use PHP's array manipulating functions
$first_posts_with_thumbs = array();
foreach ($posts as $key => $post) {
if ($post->thumbnail) {
$first_posts_with_thumbs[] = $key;
if (count($first_posts_with_thumbs) === $num_top_posts) { break; }
}
}
$posts_to_move_to_top = array();
for ($i = count($first_posts_with_thumbs) - 1; $i >= 0; $i--){
$posts[$first_posts_with_thumbs[$i]]->moved = true;
$posts_to_move_to_top[] = array_splice($posts, ($first_posts_with_thumbs[$i]), 1); // extract post from posts array
}
foreach ($posts_to_move_to_top as $post) { // put back into posts at beginning
array_unshift($posts, $post[0]);
}
return $posts;
}