我有一个自定义类型,叫做不同类别的公文包。我想显示除一个类别外的所有类别。我只想在我的页面上显示每个类别的一个项目。
我一直在忙这个,我的循环只能显示类别中的所有项目,但不能每个类别只显示一个项目。到目前为止,我可以显示类别,但它们会重复,我无法在页面上只显示一次。每页一个类别。
这是我试过的两个循环。
回路1
$args = array (
\'post_type\' => \'portfolio\',
\'category__not_in\' => array(28,30),
);
$newQuery = new WP_Query($args);
if ($newQuery -> have_posts() ):
while( $newQuery-> have_posts() ): $newQuery-> the_post();
echo get_the_term_list( $post->ID , \'portfolio_type\');
endwhile;
endif;
wp_reset_postdata();
测试回路2
$args_cat = array (
\'taxonomy\' => \'portfolio_type\',
\'exclude\' => \'28,30\',
);
$categories = get_categories($args_cat);
foreach($categories as $category):
$args = array (
\'post_type\' => \'portfolio\',
\'posts_per_page\' => 1,
);
$newQuery = new WP_Query($args);
if ($newQuery -> have_posts() ):
while( $newQuery-> have_posts() ): $newQuery-> the_post();
echo get_the_term_list( $post->ID , \'portfolio_type\');
endwhile;
endif;
wp_reset_postdata();
endforeach;
谁能帮帮我谢谢?!
SO网友:Ebelanger
$args = array(
\'taxonomy\' => \'portfolio_type\',
\'exclude\' => \'28,30\',
);
$tax = get_terms($args);
foreach($tax as $t){
echo \'<div class="span3 produto-lista">\';
$args = array(
\'tax_query\' => array(
array(
\'taxonomy\' => \'portfolio_type\',
\'field\' => \'slug\',
\'terms\' => array($t->slug)
)
),
\'posts_per_page\' => 1,
\'post_status\' => \'publish\',
\'order\' => \'DESC\',
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
?>
<a href="<?php echo get_term_link($t); ?>">
<?php
the_post_thumbnail(); ?>
<h1>
<?php
echo $t->name;
?>
</h1>
</a>
<?php
}
wp_reset_query();
echo \'</div>\';
}