WordPress:与当前帖子标签无关的(不相关)帖子

时间:2018-12-11 14:57:38

标签: wordpress

我在帖子的底部显示具有相同标签的相关帖子,但我还想添加一个没有标签的随机帖子,例如“尝试这种完全不同的事情”。

我尝试使用 tag__not_in ,但是我的代码不起作用:

$tag_id = get_queried_object()->term_id;
$args = [
    'post__not_in'        => array( get_queried_object_id() ),
    'tag__not_in'         => array( $tag_id ),
    'posts_per_page'      => 1,
    'orderby'             => 'rand'
];
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
        while( $my_query->have_posts() ) {
            $my_query->the_post(); ?>

对不起,我真的不知道如何编码,我只是尝试理解Wordpress的代码以及如何修改它。

如何将posts标签ID放入数组中?

更新>可行:

global $post;
$posttags = get_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
// just the test   echo $tag->slug; 
$tag = get_term_by('name', $tag->slug, 'post_tag');
$args = array(
    'posts_per_page' => 1,
    'tag__not_in' => array($tag->term_id),
    'orderby' => 'rand',
     );
  }
}
$query = new WP_Query($args);

1 个答案:

答案 0 :(得分:0)

您遇到什么错误?

无论如何,我认为您不需要go env gopath,因为'post__not_in'会帮您解决这个问题。

在那之后,我将查看'tag__not_in'并确保您将想要的值分配给该变量。在某处回声$tag_id = get_queried_object()->term_id;并确认它就是您想要的。

最后,我认为您需要$tag_id才能成为new wp_query( $args );

相关问题