【发布时间】:2011-08-20 12:59:07
【问题描述】:
我正在尝试从 Wordpress 中的每个父类别中列出一篇文章,但我似乎无法弄清楚如何更改此代码以排除子类别。基本上我列出了类别的名称,然后是每个父类别的帖子拇指、摘录和阅读更多按钮。我不能只按类别 id 排除子类别,因为这个网站是为稍后将添加子类别的客户提供的。请帮忙,谢谢!
$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();
【问题讨论】:
标签: wordpress categories