【问题标题】:Multiple tag id in post looppost循环中的多个标签ID
【发布时间】:2015-09-16 00:38:25
【问题描述】:

如何在 WP_Query() 中添加多个标签。所以,我有三个标签:

  1. 名字(标签 id="10")
  2. 中间名(标签 id="20")
  3. 姓氏(标签 id="30")

    $args = array( 
     'post_type' => 'post',
        'paged'=>$paged,
        'posts_per_page' => 15,             
        'orderby' => 'date',
        **'tag_id' =>  array(10,20,30),**           
        'order' => 'DESC'
        );
    

以上当然行不通。

如何显示来自多个标签的帖子?

谢谢!

【问题讨论】:

    标签: wordpress tags


    【解决方案1】:

    有一个tag__in 参数专门用于此目的:

    $args = array( 
        'post_type' => 'post',
        'paged'=> $paged,
        'posts_per_page' => 15,             
        'orderby' => 'date',
        'tag__in' =>  array(10,20,30),
    );
    

    【讨论】:

      【解决方案2】:

      如果您使用的是 WP_Query,那么您可以执行以下操作。

      //Display posts that are tagged with both tag id 37 and tag id 47:
      $query = new WP_Query( array( 'tag__and' => array( 37, 47 ) ) );
      

      或

      //To display posts from either tag id 37 or 47, you could use tag as mentioned above, or explicitly specify by using tag__in:
      $query = new WP_Query( array( 'tag__in' => array( 37, 47 ) ) );
      

      更多内容可以访问官方链接https://codex.wordpress.org/Class_Reference/WP_Query

      【讨论】:

        猜你喜欢
        • 2019-03-26
        • 2021-09-19
        • 1970-01-01
        • 2021-07-12
        • 1970-01-01
        • 1970-01-01
        • 2020-09-30
        • 1970-01-01
        • 2020-03-04
        相关资源
        最近更新 更多