修改 the_post_navigation 以在 wordpress 中包含分类术语

modify the_post_navigation to include taxonomy terms in wordpress

我修改了 the_post_navigation $args 以通过将 'in_same_term' 参数更改为 TRUE 来在特定的分类术语中导航。它工作得很好。我想做的是将分类术语名称添加到 'prev_text' 和 'next_text' 参数中,这样 link 就不会只显示 "Next: (Post Title)" 而是显示 "Next (Taxonomy Term): (Post Title)".我认为您需要从页面获取术语,将其存储在变量中,就像我已经在做的那样,以便在页面顶部显示术语名称:

<?php global $post;

$terms = get_the_terms( $post->ID , 'resource_cat' );
foreach ( $terms as $term ) {
echo $term->name;
} ?>

但是我如何在 'prev_text' 和 'next_text' 参数中获取该变量?这是我当前的代码:

$args = array(
            'prev_text'          => '&#8592;Previous<span>%title</span>',
            'next_text'          => 'Next<span>%title</span> &#8594;',
            'in_same_term'       => true,
            'taxonomy'           => 'resource_cat',
            'excluded_terms'     => 1,
            'screen_reader_text' => 'Post navigation'
        );
        the_post_navigation($args);

或者这是完全错误的方法吗?

使用以下条件的 Next link 的理想最终结果示例为:

CPT=resources (using single-resources.php template)
Taxonomy=resource_cat
Taxonomy Term=Podcasts

期望的结果:下一个播客:"Name of Podcast"

如果有人能用正确的语法帮助我或指出更好的方向,我将不胜感激!!提前致谢。

我认为如果只有一个 resource_cat 术语分配给 post:

,这样的事情应该有效
$args = array(
  'prev_text'          => '&#8592; Previous ' . $term->name . ': <span>%title</span>',
  'next_text'          => 'Next ' . $term->name . ': <span>%title</span> &#8594;',
  'in_same_term'       => true,
  'taxonomy'           => 'resource_cat',
  'excluded_terms'     => 1,
  'screen_reader_text' => 'Post navigation'
);
the_post_navigation($args);

我在自己的安装中测试了这段代码,它对我有用。

仅供参考:如果为 post 分配了多个术语,导航 link 将仅在页面顶部显示循环中最后一个术语的名称。