我在single.twig文件中遇到post.next和post.prev时遇到问题。查询是针对自定义帖子类型的,但也针对特定的分类术语。存档页面显示正确的帖子,但如果我单击一个项目并进入single.twig,则next和prev按钮会循环显示自定义帖子类型的所有帖子,而不仅仅是包含指定分类术语的帖子。这是Timber支持的东西吗?如果没有,有人可以帮我在我的functions.php中编写一个函数吗?
我甚至试过覆盖默认查询。这是我的archive.php文件中的代码:
global $paged;
if (!isset($paged) || !$paged){
$paged = 1;
}
$context = Timber::get_context();
$args = array(
'post_type' => 'credits', //this is the CPT
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => '999',
'paged' => $paged,
/*
'meta_query' => array(
array(
'key' => 'video',
'value' => 'yes',
'compare' => '=',
)
)
*/
'tax_query' => array(
array (
'taxonomy' => 'auto_taxonomies',
'field' => 'slug',
'terms' => 'in-film-archive',
)
)
);
/* THIS LINE IS CRUCIAL */
/* in order for WordPress to know what to paginate */
/* your args have to be the defualt query */
query_posts($args);
/* make sure you've got query_posts in your .php file */
$context['posts'] = Timber::get_posts();
$context['pagination'] = Timber::get_pagination();
var_dump($context['pagination']);
Timber::render('archive-films.twig', $context);
这是我在single.twig文件中的代码:
<div class="post-nav">
{% if post.prev(true) != null %}
<div class="prev-film two columns"><a href="{{post.prev.link}}">{{post.prev(true).title}}</a></div>
{% endif %}
{% if post.next(true) != null %}
<div class="next-film two columns"><a href="{{post.next.link}}">{{post.next(true).title}}</a></div>
{% endif %}
</div>
我是否需要添加特定于我的single.php文件的任何内容?
答案 0 :(得分:1)
其实我自己弄清楚了。
在函数php中我添加了:
//custom next post within taxonomy term
function custom_next_post($post_id){
$next_post = get_adjacent_post( true, '', true, 'tax slug goes here' );
if (is_a($next_post, 'WP_Post')){
echo "<a href=" . get_permalink($next_post->ID) . ">" . get_the_title($next_post->ID) . "</a>";
}
}
//custom prev post within taxonomy term
function custom_prev_post($post_id){
$next_post = get_adjacent_post( true, '', false, 'tax slug goes here' );
if (is_a($next_post, 'WP_Post')){
echo "<a href=" . get_permalink($next_post->ID) . ">" . get_the_title($next_post->ID) . "</a>";
}
}
并在single.twig文件中添加:
{# PAGINATION - THESE CALL A CUSTOM FUNCTION IN FUNCTIONS.PHP TO CYCLE ONLY WITHIN OUR TAXONOMY TERM #}
<div class="post-nav">
<div class="prev-film two columns">{{function('custom_prev_post',post.id)}}</div>
<div class="next-film two columns">{{function('custom_next_post',post.id)}}</div>
</div>