【发布时间】: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