我有代码来显示每个类别(包括子类别)的最新 3 个帖子,我想从这些子类别中排除帖子,因为这些帖子已经显示在父类别中。
例如,我将打印机类别作为父类别,将(打印机配件,.....,.....)作为子类别。
所以它显示了每个最新的 3 个帖子,我想排除任何子类别(打印机配件、.....、....)及其帖子的显示。
这是代码:
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC',
'child_of' => 0
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<dl>';
echo '<dt> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all items in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></dt>';
$post_args = array(
'numberposts' => 3,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<dd>
<div class="allincat">
<div class="catmeta">
<span class="authinfo">
<div class="authimg"></div>
<?php the_author(); ?> | <?php the_time('jS F Y') ?> </span>
</div>
<div class="allincatimg">
<?php the_post_thumbnail(array(50,50)); ?> </div>
<div class="allincattit">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div>
</div>
</dd>
<?php
}
echo '<div class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all items in %s" ), $category->name ) . '" ' . '>View all items in ' . $category->name.'</a></div>';
echo '</dl>';
}