How to create a category and sub-category sorted blogroll with all posts?

Ok, so after a lot of digging around I came up with code that does exactly what I asked for. However I don’t believe this code to be pretty/efficient/reusable in any way. I’m merely posting it since it executes what was expected. I would appreciate an answer that would point me to the “best practice” way of coding so I could learn from it, so I won’t mark my question as answered for a week or so to wait for some better coding.

This is the result I came to:

<?php
        $isOdd = true; // setup boolean to determine parent div id for CSS
        $args1 = array(
          'orderby' => 'ID',
          'order' => 'ASC',
          'hierarchical' => true
          );
        $cats = get_categories( $args1 ); // get all the categories from the database

            // loop through the categries
            foreach ($cats as $cat) {
                $cat_id= $cat->term_id;// setup the cateogory ID
                $cat_name= $cat->name;// setup the category name
                $parent= $cat->category_parent;// setup the category parent
                $cat_child= get_term_children( $cat_id, 'category' ); // outputs a string with child categories from the taxonomy


                $has_child=bool; // create a variable that will allow to verify if a category has children
                if( empty($cat_child)) { $has_child= false; }
                else { $has_child= true; }

                if ( $parent == 0 && $has_child == false ) { //is parent && no child -> loop posts
                    echo "<div class=\"parent-wrap\" id=\"". ( $isOdd ? 'odd' : 'even') . "\">";
                        echo "<h2>".$cat_name."</h2>";
                        echo "<div class=\"post-wrap\">";
                            echo "<ul class=\"post-list\">";
                                echo "<li class=\"back-button\"><</li>";

                                    // create a custom wordpress query
                                    $args2 = 'cat=" . $cat_id . "&orderby=date&order=DESC&post_per_page=-1';
                                    query_posts( $args2 );
                                    // start the wordpress loop!
                                    if (have_posts()) : while (have_posts()) : the_post(); 
                                        get_template_part( 'content', get_post_format() ); //get post template from content.php
                                    endwhile; endif;

                                echo "<li class=\"front-button\">></li>";
                            echo "</ul><!-- .post-list -->";
                        echo "</div><!-- .post-wrap -->";
                    echo "</div><!-- .parent-wrap -->";
                    $isOdd = !$isOdd;
                    }
                elseif ( $parent == 0 && $has_child == true ) { //is parent && has child -> post title
                    echo "<div class=\"parent-wrap\" id=\"". ( $isOdd ? 'odd' : 'even') . "\">";
                        echo "<h2>".$cat_name."</h2>";

                        // loop through the categries IN THE ELSEIF STATEMENT
                        foreach ($cats as $cat) {
                            $cat_id_new= $cat->term_id;// setup the new cateogory ID
                            $cat_name_new= $cat->name;// setup the new category name
                            $parent_new= $cat->category_parent;// setup the new category parent
                            $cat_child_new= get_term_children( $cat_id_new, 'category' ); // outputs a string with child categories from the taxonomy

                            $has_child_new=bool; // create a new variable that will allow to verify if a category has children
                            if( empty($cat_child_new)) { $has_child_new= false; }
                            else { $has_child_new= true; }

                            if ($parent_new != 0 && $has_child_new == false && cat_is_ancestor_of($cat_id, $cat_id_new) ) { //is child -> loop posts
                                echo "<div class=\"child-wrap\">";
                                    echo "<h3>".$cat_name_new."</h3>";
                                    echo "<div class=\"post-wrap\">";
                                        echo "<ul class=\"post-list\">";
                                            echo "<li class=\"back-button\"><</li>";

                                                // create a custom wordpress query
                                                $args2 = 'cat=" . $cat_id_new . "&orderby=date&order=DESC&post_per_page=-1';
                                                query_posts( $args2 );
                                                // start the wordpress loop!
                                                if (have_posts()) : while (have_posts()) : the_post(); 
                                                    get_template_part( 'content', get_post_format() ); //get post template from content.php
                                                endwhile;
                                                endif; // done our wordpress loop. Will start again for each category 

                                            echo "<li class=\"front-button\">></li>";
                                        echo "</ul><!-- .post-list -->";
                                    echo "</div><!-- .post-wrap -->";
                                echo "</div><!-- .child-wrap -->";
                            } // close if ($parent_new != 0...
                        } // close foreach in elseif
                    echo "</div><!-- .parent-wrap -->";
                    $isOdd = !$isOdd;
                } // close if ($parent == 0...
            } // close the 1st foreach statement ?>

I feel some of the things in this code could be better organized if functions were created and then reused throughout the code and I’m still thinking about replacing query_posts(); with get_posts(); or pre_get_posts();.

Leave a Comment