• Hey everybody,

    I’m trying to solve this for a while, but I couldn’t get a satisfactory way to get what I want.
    Maybe it’s very simple and I’m just too inexperient.

    I need a way to display all the article titles of the child categories of a given category, like this:

    Category “x”
    Subcategory 1
    Post 1
    Post 2
    etc
    Subcategory 2
    Post 1
    Post 2

    etc

    I really can’t do this using my limited knowledge. All I could get was child category listing (without the post titles) or a listing of every category, subcategory and posts of the blog. I need a way to limit the listing to the subcategories of the category I’m navigating (so if I’m navigating in the “?cat=12” I get only a listing of the child categories of the category 12 and their post titles).

    Could anyone help me with this? Thank you very much in advance!

    Alex

Viewing 5 replies - 1 through 5 (of 5 total)
  • Could try this:

    <?php
    //for each child of category 12, show title of posts
    $cat_args=array(
      'child_of' => 12,
      '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
    ?>

    @michaelh That is one nifty piece of code. Was looking to do the same thing, dropped your code in and bang! current list of child category posts on a main category page.

    Thanks

    Hi Michael. The above code works really well. I’m trying to modify it slightly and wondered if you had any ideas:

    Category X
    -child 1 of cat x
    –subcat of child 1
    —post in subcat of child 1
    -child 2 of cat x
    –subcat of child 2
    —post in subcat of child 2

    If I’m trying to list only the latest post from each of the child categories – which I can do but if (taking the example directory structure above) I want only to list the latest post in the subcats not in the direct child – if that makes sense? If say ‘child of cat x’ has a post in it, i don’t want to list it, I only want the latest post from the subcat of the child.

    thanks in advance for your help
    Vinny

    It’s ok I’ve sorted it, if anyone wants it:
    At the top when you specify the category you want it to check, note down number and then enter it into the category_parent line.
    (I commented out the category links).

    <?php
    //for each child of category 12, show title of posts
    $cat_args=array(
      'child_of' => 24,
      'orderby' => 'name',
      'order' => 'ASC'
       );
    $categories=get_categories($cat_args);
      foreach($categories as $category) {
        $args=array(
          'showposts' => 1, // changed from -1 to show only one post
          'category__in' => array($category->term_id),
          'caller_get_posts'=>1,
    
    	   );
        $posts=get_posts($args);
          if ($posts) {
            //echo '<p><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
            echo "<ul class='gal'>";
    		foreach($posts as $post) {
              setup_postdata($post); ?>
               <?php if ($category->category_parent != '24') { ?>
    
              <li><?php the_post_thumbnail(); ?><br /><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php }?>
              <?php
            } // foreach($posts
    		echo "</ul>";
          } // if ($posts
        } // foreach($categories
    ?>

    Hi, where i have to copy this code?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Need help with category listing!’ is closed to new replies.