首页 文章

Wordpress :( un-)相关帖子不在当前帖子标签中

提问于
浏览
0

我在帖子的底部显示了相同标签的相关帖子,但我还想添加一个没有标签的随机帖子,比如“试试这个完全不同的东西” .

我尝试过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

    你得到的错误是什么?

    无论如何,我认为你不需要 'post__not_in' 因为 'tag__not_in' 会为你照顾 .

    之后,我会查看 $tag_id = get_queried_object()->term_id; 并确保您将所需的值分配给该变量 . Echo $tag_id 某处并确认它是你想要的 .

    最后,我认为你需要 new wp_query( $args );new WP_Query( $args );

相关问题