2
我试图列出每个父类别在WordPress中的一篇文章,但我似乎无法弄清楚如何改变此代码以排除子类别。基本上,我列出了类别的名称,然后列出了每个父类别的后缀缩略图,摘录和读取更多按钮。我不能只是按类别id排除子类别,因为这个网站是针对稍后将添加子类别的客户。请帮忙,谢谢!WordPress的每个父类别查询一个职位(排除子类别)
$num_cats_to_show = 10;
$count = 0;
$taxonomy = 'category';// e.g. post_tag, category
$param_type = 'category__in'; // e.g. tag__in, category__in
$term_args=array(
'orderby' => 'name',
'order' => 'ASC',
);
$terms = get_terms($taxonomy, array('exclude' => '15,1'),$term_args);
if ($terms) {
foreach($terms as $term) {
$args=array(
"$param_type" => array($term->term_id),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'caller_get_posts'=> 1,
);
$my_query = null;
$my_query = new WP_Query($args);
if($my_query->have_posts()) {
$count ++;
if ($count <= $num_cats_to_show) {
while ($my_query->have_posts()) : $my_query->the_post();
哦,你是对的。在问这个问题之前,我实际上已经尝试了你的代码,但是由于排除了条款代码(顺便说一句,但是禁用了parent = 0代码),所以它不起作用......所以我该如何拒绝这个类别。这个问题,它是“特色”类别,我仍然希望列出类别中的项目,我只是不希望类别本身列出。就像我之前说过的,我从每个父类别中列出一篇文章,但我需要从此列表中排除“精选”类别。 – 2011-05-09 07:02:22
想通了,谢谢你的超级帮助!如果有人遇到这个问题,可以这样做:'$ term_args = array( 'orderby'=>'name', 'order'=>'ASC', 'parent'=> 0, \t'exclude'=> '15', ); $ terms = get_terms($ taxonomy,$ term_args);' – 2011-05-09 07:13:21