• Resolved Suxa23

    (@suxa23)


    Hello, I’m hoping someone would be kind enough to lend me a hand, I have been searching for a solution for the past days but no luck.

    I’m trying to display the latest post from the subcategories(children) of one of my categories. So, far the code I’m using works, but it also displays the grandchildren, I tried adding the “array => depth” to show only the top level but it doesn’t do anything. Here’s the code I’m using right now:

    <?php
    $cat_args=array(
      'child_of' => '7',
      'orderby' => 'id',
       );
    $categories=get_categories($cat_args);
      foreach($categories as $category) {
        $args=array(
          'showposts' => 1,
          'category__in' => array($category->term_id),
          'caller_get_posts'=>1
        );
        $posts=get_posts($args);
          if ($posts) {
            echo '<div class="new-entry"><h2><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h2> ';
            foreach($posts as $post) {
              setup_postdata($post); ?>
    
              <div class="new-date">Latest Entry</div>
    
              <div class="new-detail">
              <div class="new-img"><?php echo get_the_post_thumbnail($id, array(200,200)) ?></div>
              <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
              <?php the_excerpt(); ?>
              </div>
    
              </div>
              <?php
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>

    So in other words this what I’m trying to achieve:

    Category 1 (page content)
    – Subcategory 1(show latest entry)
    – Subcategory 2(show latest entry)
    — Subcategory 1 (don’t show on this page)
    — Subcategory 2 (don’t show on this page)
    – Subcategory 3(show latest entry)
    – Subcategory 4(show latest entry)

    Any feedback will be greatly appreciate it!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Use parent in place of child_of… child will not consider depth (if it’s a child 3 levels deep it’s still a child), parent will only match those categories that have the assigned category as a direct parent..

    Thread Starter Suxa23

    (@suxa23)

    Yes! That works perfectly, thank you so much Mark!

    You’re welcome.. ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display only the posts of the top level category’ is closed to new replies.