Forums

Display latest 5 posts on Parent Categories (5 posts per child category only) (14 posts)

  1. heronakamura
    Member
    Posted 11 months ago #

    Hi there,

    I have 8 parent categories but all posts are under the child categories. I want to display the latest 5 posts per child categories in that parent category page.

    Parent Category 1 (http://example.com/category/parent-category-1/)
    -- Sub Category 1
    (show 5 posts here)
    -- Sub Category 2
    (show 5 posts here)
    -- Sub Category 3
    (show 5 posts here)
    -- Sub Category 4
    (show 5 posts here)
    -- Sub Category 5
    (show 5 posts here)

    Parent Category 2
    -- Sub Category 1
    (show 5 posts here)
    -- Sub Category 2
    (show 5 posts here)
    -- Sub Category 3
    (show 5 posts here)
    -- Sub Category 4
    (show 5 posts here)
    -- Sub Category 5
    (show 5 posts here)

    Parent Category 3
    same as above

    What I have is this:

    <?php
    //for each category, show 1 post
    $cat_args=array(
      'orderby' => 'name',
      'order' => 'ASC'
       );
    $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 '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
            foreach($posts as $post) {
              setup_postdata($post); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
              <?php
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>

    But this code displays posts on every single parent and child categories. Can anyone help me to only display posts of child categories of the parent category it is under.

    Many Thanks in advance.

  2. keesiemeijer
    moderator
    Posted 11 months ago #

    Try something like this:

    <?php
      foreach(get_categories() as $category){
        // check if it's a parent category
        if($category->parent == 0) {
          echo '<p>Category: <a href="' . get_category_link( $category->cat_ID ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    
          // get category children
          $child_categories= get_categories('child_of='.$category->cat_ID);
          foreach($child_categories as $child_cat){
            $posts= get_posts('posts_per_page=5&cat='.$child_cat->cat_ID);
            if ($posts) {
              echo '- <a href="' . get_category_link( $child_cat->cat_ID ) . '" title="' . sprintf( __( "View all posts in %s" ), $child_cat->name ) . '" ' . '>' . $child_cat->name.'</a> </p> ';
              foreach($posts as $post) {
                setup_postdata($post);
                ?>
                <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
                <?php
              }
            }
          }
        }
      }
      ?>
  3. heronakamura
    Member
    Posted 11 months ago #

    Hiya,

    Thank you for your reply. Unfortunately, this still displays all the parent categories and 5 posts for all its children category. Is there any way to only add the children categories of the parent category your viewing?

  4. keesiemeijer
    moderator
    Posted 11 months ago #

    Am I correct that you dont want links to the parent categories? Just the current category title and 5 links to latest posts of that category.

  5. heronakamura
    Member
    Posted 11 months ago #

    Sorry to confuse you. I have created separate category files for each of the parent categories. Like category-1.php etc...

    On my Parent Category 1 Page, your code shows this:

    Category: Parent Category 1 (http://example.com/category/parent-category-1/)
    -- Sub Category 1
    5 posts is shown
    -- Sub Category 2
    5 posts is shown
    -- Sub Category 3
    5 posts is shown

    But it also shows the other parent categories:

    Parent Category 2
    Parent Category 3
    Parent Category 4
    Parent Category 5

    How can we exclude the other parent categories?

    Many Thanks in advance.

  6. heronakamura
    Member
    Posted 11 months ago #

    To make it a bit more clear: Lets say I have an animal website with parent categories such as Dogs, Cats, Horses etc....

    Your code display it in this way:

    This is the Dog Category Page

    Category: Cats
    Category: Dogs
    - Hounds (Child Category)
    5 posts is shown
    - Basset Hound (Grandchild Category)
    5 posts is also shown
    - Terrier (Child Category)
    5 posts is shown
    - Yorkshire Terrier (Grandchild Category)
    5 posts is also shown
    Category: Horses

    How can we exclude the cats and horses considering I'm on the dogs category page? Also, only display the child categories and 5 posts and not display the grandchild categories.

    Many Thanks. I appreciate your hepl and patience greatly.

  7. keesiemeijer
    moderator
    Posted 11 months ago #

    Try it with this:

    <?php
      $category = get_category(get_query_var('cat'));
      echo '<p>Category: <a href="' . get_category_link( $category->cat_ID ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    
      // get category children
      $child_categories= get_categories('child_of='.$category->cat_ID);
      foreach($child_categories as $child_cat){
        $posts= get_posts('posts_per_page=5&cat='.$child_cat->cat_ID);
        if ($posts) {
          echo '-- <a href="' . get_category_link( $child_cat->cat_ID ) . '" title="' . sprintf( __( "View all posts in %s" ), $child_cat->name ) . '" ' . '>' . $child_cat->name.'</a> </p> ';
          foreach($posts as $post) {
            setup_postdata($post);
            ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php
          }
        }
      }
      ?>
  8. heronakamura
    Member
    Posted 11 months ago #

    We're getting there mate.

    Your code display it in this way:

    This is the Dog Category Page

    Category: Dogs
    - Hounds (Child Category)
    5 posts is shown
    - Basset Hound (Grandchild Category)
    5 posts is also shown

    - Terrier (Child Category)
    5 posts is shown
    - Yorkshire Terrier (Grandchild Category)
    5 posts is also shown

    How can we exclude the Grandchild Category and its 5 posts?

    I really appreciate it. Also, if you're happy for me to quote you, can I have your website link, so I can quote you when someone needed this code. Many Thanks in advance.

  9. keesiemeijer
    moderator
    Posted 11 months ago #

    change this

    $child_categories= get_categories('child_of='.$category->cat_ID);

    to this:

    $child_categories= get_categories('parent='.$category->cat_ID);
  10. heronakamura
    Member
    Posted 11 months ago #

    This worked as I wanted to. Many Thanks indeed. Appreciate it greatly. I spent weeks trying to figure it out with no luck.

    You're an ace keesiemeijer. Thaks again. Just like I said if you have a website, I'll be happy to quote you if someone ask for this code.

  11. keesiemeijer
    moderator
    Posted 11 months ago #

    No need to quote me. Glad you got it solved.

  12. heronakamura
    Member
    Posted 11 months ago #

    cheers mate.

  13. heronakamura
    Member
    Posted 11 months ago #

    Sorry mate. I thought it's all perfect but I just realised, one of the child category (tips on dog care) did not show up. I have 3 child categories under parent category Dogs. namely hounds, terrier and tips on dog care.

    This is how it shows with your code:

    Category: Dogs
    - Hounds (Child Category)
    5 posts is shown
    - Terrier (Child Category)
    5 posts is shown
    - Tips on Dog Care (Child Category) This is missing

    I'm assuming that the reason why it was dropped because there was no posts filed directly under Tips on Dog Care child category. This is how the posts are organised:

    Dogs (Parent Category)
    - Hounds (Child Category) - posts are filed under this child category
    - Terrier (Child Category) - posts are filed under this child category
    - Tips on Dog Care (Child Category) - there are no posts filed under this child category. Instead it is filed under Tips on Dog Care sub-vategory namely:
    --- Boarding Kennels (Sub-Category of Dog Tips Chld Category) - posts are here instead
    --- Dog Insurance (Sub-Category of Dog Tips Chld Category) - posts are here instead

    Is there anyway to show it this way:

    Category: Dogs
    - Hounds (Child Category)
    5 posts is shown
    - Terrier (Child Category)
    5 posts is shown
    - Tips on Dog Care (Child Category) Show this
    5 posts is shown. But pull it from all the Tips on Dog care sub-categories

  14. keesiemeijer
    moderator
    Posted 11 months ago #

    You can change this to also show the empty categories:

    $child_categories= get_categories('parent='.$category->cat_ID);

    to this:

    $child_categories= get_categories('hide_empty=0&parent='.$category->cat_ID);

    But that will not show the sub categories and the posts links. I don't have the time now to make it also show sub categories and the posts links.

Reply

You must log in to post.

About this Topic