【问题标题】:display posts, categories, pages together - wordpress一起显示帖子、类别、页面 - wordpress
【发布时间】:2016-11-07 14:50:44
【问题描述】:

我正在收集统计数据,我想在那里显示我拥有的所有页面、帖子、分类类别。

我可以显示帖子类型和页面,因为实际上它们都有一些帖子类型,但不能同时显示分类类别:

<?php 
    $excluded_ids = array(1, 5);
    $postArgs = array(
        'post_type' => array('page', 'products'),
        'order' => 'ASC',
        'orderby' => 'title',
        'post__not_in' => $excluded_ids,
        'posts_per_page'=> -1,
        /*'taxonomy' => 'product-category'*/
    );
    $postList = get_posts($postArgs);
?>

有没有办法通过单个查询而不是多个查询来显示所有内容(帖子、类别、页面)?有什么想法吗?

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    get_posts 方法不接受数组作为 post_type 值。您应该使用WP_Query 来查询帖子。

    $args = array(
        'post_type' => array( 'page', 'products'  )
    );
    $query = new WP_Query( $args );
    

    【讨论】:

      【解决方案2】:

      如果我理解正确的话

      $argss = array(
      'post_type' => array('page', 'products'),
      'order' => 'ASC',
      'orderby' => 'title',
      'tax_query' => array(
          array(
              'taxonomy' => 'your taxonomies',
              'field'    => 'slug',
              'terms' => 'your term',
          ),
      ),
      );
      
      $the_queryy = new WP_Query( $argss );
      
      if ( $the_queryy->have_posts() ) {
      while ( $the_queryy->have_posts() ) {
          $the_queryy->the_post();
                            // echo stuff
      }
      wp_reset_postdata();
      }
      

      【讨论】:

      • 感谢您的回复。但它不起作用。它显示页面、产品但不显示类别。 @Tauras
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多