2

I am trying to make a custom sidebar for my posts in certain categories. It will look something like this.

enter image description here

My category structure looks like this:

  • Parent 1
    • Child 1
      • Sub Child 1
      • Sub Child 2
    • Child 1
      • Sub Child 1
      • Sub Child 2
  • Parent 2

What I want to do is to display all posts under Sub Child 1 and 2. I will be more specific, lets say we have 3 posts, 2 of them is under sub child 1 and last one is under sub child 2. Then I only want the 2 posts that is under sub child 1 to be displayed under Sub child 1, and the 3 :rd post to display only under sub child 2.

I have been looking in 2 days now over the Internet to figure this out how to do this, but I dont get what I am doing wrong.

Here is my code and what I am trying to modify:

<?php 
$cat = get_the_category();
$catID = get_cat_ID($cat);
$subcats = get_categories('child_of=' . $catID);
    foreach($subcats as $subcat) {
    echo '<h3>';
    echo '<a href="' . get_category_link($subcat->cat_ID) . '">';
    echo '/' . $subcat->cat_name . '';
    echo '</a></h3>';
    echo '<ul>';
    $subcat_posts = get_posts('cat=' . $subcat->cat_ID);
    foreach($subcat_posts as $subcat_post) {
        $postID = $subcat_post->ID;
    echo '<li>';
    echo '<a href="' . get_permalink($postID) . '">';
    echo get_the_title($postID);
    echo '</a></li>';
    }
    echo '</ul>';
} 
?>

Any ideas?

3 Answers 3

4

I guess you could take a big step using wp_list_categories() with a walker to add a additional Unordered List to every item.

The code:

$cat_id = get_query_var( 'cat' );
$subcats = get_categories( 'child_of=' . $cat_id ); // child categories

class Cat_Walker extends Walker_Category {
    function end_el( &$output, $page, $depth = 0, $args = array() ) {
        $posts = get_posts( 'cat=' . $page->term_id );

        if ( sizeof( $posts ) > 0 ) {
            $output .= '<ul>';

            foreach ( $posts as $post ) {
                $output .= sprintf( '<li><a href="%1$s">%2$s</a></li>', get_permalink( $post->ID ), $post->post_title );
            }

            $output .= '</ul>';
        }

        $output .= '</li>';
    }
}

foreach ( $subcats as $subcat ) {
    $subsubcats = get_categories( 'child_of=' . $subcat->term_id ); // sub child categories

    foreach ( $subsubcats as $subsubcat ) {
        $args = array(
            'title_li'         => '',
            'show_option_none' => '',
            'taxonomy'         => 'category',
            'child_of'         => $subsubcat->term_id,
            'walker'           => new Cat_Walker( )
        );

        wp_list_categories( $args );
    }
}
8
  • Thanks for reply, I would call myself a noob on php, but how does the walker work? What do I have to pass with args to make it work? Mar 8, 2013 at 11:54
  • I added some arguments to $args for you, this will give you a clean Unordered List wich you can style with CSS to get the look you want. For more arguments, see this page in the Codex. Mar 8, 2013 at 12:05
  • It shows parent categorys, and for each category it displays the post under it, how do i exclude the parent and child. So that I only have Sub Child category. And it also displays every single category on the site. How do I display all sub cateogries in Child 1, 2 or 3? Thanks for the reply! Mar 8, 2013 at 14:06
  • I altered the code a little, now you will have an Unordered List for every sub child category with their posts. Mar 8, 2013 at 14:38
  • How do I make it show only all the sub categories that are under parent category (Child 1), now it shows all of sub categories. It will show All Sub categories only under child 1, becuse the post is under child 1. So then you can easy go to another sub category if you like. Thanks for reply, this means alot! Mar 8, 2013 at 14:54
1

Finally, after searching all day .. collecting & combine the code.. this is it :

<?php $categories =  get_categories('child_of='.get_queried_object()->term_id); if(!empty($categories)):  
        foreach ($categories as $category) {
        $category_id = $category->term_id;
        query_posts('cat='.$category_id."&order=ASC");
        if ( have_posts() ) :
           ?>        <h3><?php echo $category->name; ?><span></span></h3><?php while ( have_posts() ) : the_post(); ?><ul><li><a href="<?php the_permalink();?>"><?php the_title(); ?></a></li></ul><?php endwhile;?><?php endif; 
    } endif; ?>

enjoy it :)

0

Function for getting all categories and their post for a particular parent category

function get_cat_subcat_posts($catid){
            $categories =  get_categories('child_of='.$catid);

            if(!empty($categories)):  

                foreach ($categories as $category) {

                $category_id = $category->term_id;

                query_posts('cat='.$category_id."&order=ASC");
                if ( have_posts() ) :
            ?>

                 <div class="detail_row">
                 <h3><?php echo $category->name; ?><span></span></h3>
                    <ul>
                <?php 

                // Start the Loop.
                while ( have_posts() ) : the_post();            
                ?>

                      <li><a href="<?php the_permalink();?>"><?php the_title(); ?></a></li>

                     <?php
                        endwhile;

                    ?>
                 </ul>
                  </div>

            <?php
                endif; 
                wp_reset_query();

                // Recall the category posts function
                get_cat_subcat_posts($category_id);

                }

            endif;

            }
3
  • This function can display multilevel category and their posts thanks. copy this from function get_cat_subcat_posts($catid){ Sep 23, 2014 at 10:04
  • Call the Function like $catid = get_cat_ID('Bible Studies'); get_cat_subcat_posts($catid); Sep 23, 2014 at 10:05
  • Please: Additional infos as edits to your answer.
    – kaiser
    Sep 23, 2014 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.