我有一个列表,上面有简短的帖子。
短篇文章不会显示在单个页面中,因为它们太短了。我使用ACF字段(复选框类型)来定义简短帖子:article_short
但是当我在显示长帖子的单个页面中时,我想显示下一个/上一个长帖子。
我写了:
$context['prev_next_posts'] = Timber::get_posts(array(
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'article_short',
'compare' => 'LIKE',
'value' => 0
)
),
'orderby' => 'date',
'order' => 'DESC',
'has_password' => FALSE
));
短帖子被排除在外。
在我的树枝文件中,附加了上下文:
{% if prev_next_posts.next %}
<a href="{{ prev_next_posts.next.link }}">{{ prev_next_posts.next.title }}</a>
{% endif %}
{% if prev_next_posts.prev %}
<a href="{{ prev_next_posts.prev.link }}">{{ prev_next_posts.prev.title }}</a>
{% endif %}
但是什么也没显示...请问您有任何想法吗?
根据Timber文档,我也尝试使用(true)
显示同一类别的帖子。结果相同。什么也没显示。
https://timber.github.io/docs/reference/timber-post/#next
{% if prev_next_posts.next(true) %}
<a href="{{ prev_next_posts.next.link }}">{{ prev_next_posts.next.title }}</a>
{% endif %}
{% if prev_next_posts.prev(true) %}
<a href="{{ prev_next_posts.prev.link }}">{{ prev_next_posts.prev.title }}</a>
{% endif %}
答案 0 :(得分:0)
您当前的问题是prev_next_posts
只是您所有长篇文章的数组。 POST.next
和POST.prev
旨在用于单个post对象。
不幸的是,get_adjacent_post()
开箱即用,只能按术语,类别等进行排除,而不能按meta_key
进行排除。
这是解决您要完成的任务的快速方法:
single.php:
// Get all long articles, but only their IDs
$long_articles = get_posts(array(
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'article_short',
'compare' => 'LIKE',
'value' => 0
)
),
'orderby' => 'date',
'order' => 'DESC',
'has_password' => FALSE,
'fields' => 'ids', // Only get post IDs
'posts_per_page' => -1
));
// Get the current index in the array of long article IDs
$current_index = array_search($post->ID, $long_articles);
// Get the previous post if it exists
if (isset($long_articles[$current_index - 1])) {
$context['previous_post'] = Timber::get_post($long_articles[$current_index - 1]);
}
// Get the next post if it exists
if (isset($long_articles[$current_index + 1])) {
$context['next_post'] = Timber::get_post($long_articles[$current_index + 1]);
}
然后插入single.twig:
{# Previous post link if it exists #}
{% if previous_post %}
<div>
<a href="{{ previous_post.link }}">Previous Post: {{ previous_post.title }}</a>
</div>
{% endif %}
{# Next post link if it exists #}
{% if next_post %}
<div>
<a href="{{ next_post.link }}">Next Post: {{ next_post.title }}</a>
</div>
{% endif %}