【问题标题】:Wordpress loop how to get tag slug and be value in query_postsWordpress 循环如何获取标签 slug 并在 query_posts 中获得价值
【发布时间】:2013-08-10 14:59:29
【问题描述】:

我想做的是:

  • 在类别 1 的帖子中,每个帖子都有一个标签
  • 现在在另一个类别下的另一个帖子中,我想调用类别 1 中具有相同标签的帖子,例如 cat=1&tag=1 和 cat=2&tag=1

这是代码,但它不起作用:

<?php

            $t = wp_get_post_tags($post->ID);

                         query_posts( 'cat=45&tag=' . $t. '' );
                                        // The Loop
     while ( have_posts() ) : the_post(); ?>

【问题讨论】:

    标签: post get tags wordpress


    【解决方案1】:

    您必须在query_posts 中传递tag id 并且wp_get_post_tags() 返回数组而不是id,因此最好通过标签实现帖子,您必须传递标签id

    <?php
    
         $t = wp_get_post_tags($post->ID);
    
         query_posts( 'cat=45&tag=' . $t[0]->term_id. '' );
                                            // The Loop
         while ( have_posts() ) : the_post(); ?>
    

    可以有多个标签,因此您必须循环通过$t 来获取所有标签ID

    请参考本手册wp_get_post_tags

    【讨论】:

      猜你喜欢
      • 2013-11-29
      • 2017-11-26
      • 1970-01-01
      • 2021-08-14
      • 2021-03-28
      • 1970-01-01
      • 2016-08-12
      • 2015-09-16
      • 1970-01-01
      相关资源
      最近更新 更多