2012-05-31 218 views
1

我有代码显示每个类别(包括子类别)的最新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>'; 
} 

回答

0

查询职位。

<?php 
    $args = array(
     'cat'  => 22, // your parent cat id here 
     'order' => 'ASC' 
     'orderby' => 'name', 
     'posts_per_page' => 3 
    ); 

    query_posts($args); ?> 

      <?php if (have_posts()) : ?> 
      <?php while (have_posts()) : the_post(); ?> 
       <dd> 
       <div class="allincat"> 
        <!-- content here --> 
       </div> 
       </dd> 
      <?php endwhile; ?> 
      <?php endif; ?> 
+0

谢谢你,但,这将通过ID 站点中的每台父类中定义,但我想自动为我的所有父类未ID,因为父类可能会更晚一些。任何解决方案 – dina

+0

使用'$ cat_ID = get_query_var('cat');'动态获取当前类别ID然后 – anuragbh

+0

请您解释这个解决方案 – dina