【问题标题】:Wordpress: get-posts is emptyWordpress:get-posts 为空
【发布时间】:2016-03-27 19:26:40
【问题描述】:

我在 Wordpress 中创建了一个自定义帖子类型并为其创建了一个分类。现在我想显示分类术语,并在每个术语下显示具有该术语的帖子。 我正在尝试使用 get_posts ,但 get-posts 只是显示为空。这是我与 cmets 的代码:

<?php
//for each category, show all posts
$cat_args=array(
  'orderby' => 'name',
  'order' => 'ASC',
  'taxonomy' => 'teachres-categorie',
  'post_type'=> 'biology'
);

$categories=get_categories($cat_args);

foreach($categories as $category) {
$args=array(
   'showposts' => -1,
   'category' => array($category->term_taxonomy_id),
   'ignore_sticky_posts'=>1,
   'post_type'=> 'biology', 
   'taxonomy' => 'teachres-categorie'
 );
 ?> 

 <h2><?php  echo '<p>Category: <a href="' . get_category_link($category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';?></h2>
 <p><?php echo $category->term_taxonomy_id; ?></p> 

 //this gets displayed just fine, meaning $category-array is filled

 <?php
 $posts = get_posts($args);

 //at this point $args is filled: Array ( [showposts] => -1 [category] => Array ( [0] => 34 ) [ignore_sticky_posts] => 1 [post_type] => biology [taxonomy] => teachres-categorie ) 
 //at this point $posts is empty: Array ( ) 

   if ($posts) {

    //This never gets executed as $posts is empty

     foreach($posts as $post) {
    // ...so we'll never get to here            

     } // end foreach($posts
   } // end if ($posts
 } // // end foreach($categories
?>

除此代码外,我没有更改主要的后查询,这些是页面上唯一的循环。当我对此进行搜索时,有更多人遇到同样的问题,但他们的解决方案都对我的情况没有帮助。

谁能告诉我为什么 get_posts 是空的?

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    我会使用 tax_query 而不是您尝试在帖子参数中按类别获取帖子的方式。 posts_per_page 也替换了 showposts,但它可能不是那么重要。试试这个:

    $args = array(
       'posts_per_page' => -1,
       'ignore_sticky_posts' => 1,
       'post_type'=> 'biology', 
       'tax_query' => array(
           array(
           'taxonomy' => 'teachres-categorie',
           'field' => 'term_id',
           'terms' => $category->term_id,
           ),
        )
    );
    
    $posts = get_posts($args);
    

    这些 args 将查询具有 term_id(循环内类别的 id)的所有帖子,以检索该类别术语中的所有帖子。

    @vard 也对 category 参数提出了一个很好的观点,我认为它回答了你关于为什么它可能不起作用的问题。

    【讨论】:

      【解决方案2】:

      get_posts 参数中几乎没有错误。考虑到您实际上具有以下基于您的 cmets:

      $posts = get_posts(array(
         'showposts' => -1,
         'category' => array(0 => 34),
         'ignore_sticky_posts' => 1,
         'post_type'=> 'biology', 
         'taxonomy' => 'teachres-categorie'
      ));
      
      • category 参数需要一个整数(类别 ID)或包含逗号分隔的类别 ID 列表的字符串。 From the doc:

        category 参数可以是逗号分隔的类别列表,因为get_posts() 函数将'category' 参数作为'cat' 直接传递给WP_Query

        然后,如果您查看WP_Query 文档,您将看到类别的数组值仅适用于category__andcategory__incategory__not_in 参数。我认为这就是您的查询首先没有检索到任何帖子的原因。

      • taxonomy 参数不存在。在发送分类 slug 之前有一个 tax 参数,但自 3.1 版以来已被弃用。如果您想根据分类查询您的帖子,请改用tax_query 参数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-07
        • 2012-05-25
        • 1970-01-01
        • 2020-01-10
        • 2014-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多