0

我的 single.twig 文件中的 post.next 和 post.prev 有问题。该查询针对自定义帖子类型,但也针对特定分类术语。存档页面显示正确的帖子,但如果我单击一个项目并进入 single.twig,下一个和上一个按钮会循环显示自定义帖子类型的所有帖子,而不仅仅是包含指定分类术语的帖子。这是 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 文件的内容吗?

4

1 回答 1

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> 
于 2017-08-08T14:42:52.197 回答