Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want the archive page of custom page having custom post type in order with custom taxonomy. like

-parent1_taxonomy
   -child1_taxonomy
     -child1_taxonomy_post1
     -child1_taxonomy_post2
           -child2_taxonomy
                 -child2_taxonomy_post1
                 -child2_taxonomy_post2
parent2_taxonomy
 -likewise
  ................

I use following code for archive template.

<?php
/**
*Display categories list and post
*/

$query_category = mysql_query("SELECT wp_terms.term_id,wp_terms.name, wp_term_taxonomy.parent FROM wp_terms INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id WHERE wp_term_taxonomy.taxonomy = 'food_menu_food_category' ORDER BY name");
    $data = array();
    $index = array();
    while ($row = mysql_fetch_assoc($query_category)) {
        $id = $row["term_id"];
        $parent_id = $row["parent"] === NULL ? "NULL" : $row["parent"];
        $data[$id] = $row;
        $index[$parent_id][] = $id;
    }
    function display_child_nodes($parent_id, $level){
        global $data, $index;
        $parent_id = $parent_id === NULL ? "NULL" : $parent_id;
        if (isset($index[$parent_id])) {
            echo "<ul>";
            foreach ($index[$parent_id] as $id) {
                if($level == 0){
                    echo "<li><h3><strong>".$data[$id]["name"]."</strong>";
                    //$ca=$data[$id]["name"]; 



                            ?>

    <?php
                }
                if($level == 1){
                    echo "</li><li><h3><strong>".$data[$id]["name"]."</strong></h3></li>";
                      $ch=$data[$id]["name"]  ;
                      //echo $ch;

                        //For post content 
                            $post_type = 'food_menu';
                            $tax = 'food_menu_food_category';
                            $tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC'));
                            if ($tax_terms) {
                                foreach ($tax_terms as $tax_term) {
                                    $args = array(
                                        'post_type' => $post_type,
                                        "$tax" => $tax_term->slug,
                                        'post_status' => 'publish',
                                        'posts_per_page' => - 1,
                                        'orderby' => 'title',
                                        'order' => 'ASC',
                                        'caller_get_posts' => 1
                                        ); // END $args
                                    $my_query = null;
                                    $my_query = new WP_Query($args);
                                    if ($my_query->have_posts()) {
                                        //echo '<h1>' . $tax_term->name . '</h1>';
                                        $c=$tax_term->name;
                                        //echo $c;
                                        while ($my_query->have_posts()) : $my_query->the_post();
                                        if($ch==$c){
                                            //echo '<h1>'. $ch .'<h1>';
                                            ?><ul><li> <a href="<?php the_permalink(); ?>">
                                            <?php $scat=get_the_title();
                                            echo $scat; ?></a>
                                            <a href="<?php the_permalink(); ?>">
                                            <?php echo esc_html( get_post_meta( get_the_ID(), 'food_price', true ) ); ?> </a>
                                            </li></ul>
                                       <?php     
                                        }
                                        ?>
                                      <?php
                                        endwhile;
                                    } // END if have_posts loop
                                    wp_reset_query();
                                } // END foreach $tax_terms
                            } // END if $tax_terms

                }



                // Child of child class
                if($level == 2){
                    echo "</li><ul><li><h3><strong>".$data[$id]["name"]."</strong></h3></ul></li>";
                      $ch=$data[$id]["name"]  ;
                      //echo $ch;

                        //For post content 
                            $post_type = 'food_menu';
                            $tax = 'food_menu_food_category';
                            $tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC'));
                            if ($tax_terms) {
                                foreach ($tax_terms as $tax_term) {
                                    $args = array(
                                        'post_type' => $post_type,
                                        "$tax" => $tax_term->slug,
                                        'post_status' => 'publish',
                                        'posts_per_page' => - 1,
                                        'orderby' => 'title',
                                        'order' => 'ASC',
                                        'caller_get_posts' => 1
                                        ); // END $args
                                    $my_query = null;
                                    $my_query = new WP_Query($args);
                                    if ($my_query->have_posts()) {
                                        //echo '<h1>' . $tax_term->name . '</h1>';
                                        $c=$tax_term->name;
                                        //echo $c;
                                        while ($my_query->have_posts()) : $my_query->the_post();
                                        if($ch==$c){
                                            //echo '<h1>'. $ch .'<h1>';
                                            ?><ul><ul><li> <a href="<?php the_permalink(); ?>">
                                            <?php $scat=get_the_title();
                                            echo $scat; ?></a>
                                            <a href="<?php the_permalink(); ?>">
                                            <?php echo esc_html( get_post_meta( get_the_ID(), 'food_price', true ) ); ?> </a>
                                            </li></ul></ul>
                                       <?php     
                                        }
                                        ?>
                                      <?php
                                        endwhile;
                                    } // END if have_posts loop
                                    wp_reset_query();
                                } // END foreach $tax_terms
                            } // END if $tax_terms

                }




                //Child of child of child class
                if($level == 3){
                    echo "</li><ul><ul><li><h3><strong>".$data[$id]["name"]."</strong></h3></ul></ul></li>";
                      $ch=$data[$id]["name"]  ;
                      //echo $ch;

                        //For post content 
                            $post_type = 'food_menu';
                            $tax = 'food_menu_food_category';
                            $tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC'));
                            if ($tax_terms) {
                                foreach ($tax_terms as $tax_term) {
                                    $args = array(
                                        'post_type' => $post_type,
                                        "$tax" => $tax_term->slug,
                                        'post_status' => 'publish',
                                        'posts_per_page' => - 1,
                                        'orderby' => 'title',
                                        'order' => 'ASC',
                                        'caller_get_posts' => 1
                                        ); // END $args
                                    $my_query = null;
                                    $my_query = new WP_Query($args);
                                    if ($my_query->have_posts()) {
                                        //echo '<h1>' . $tax_term->name . '</h1>';
                                        $c=$tax_term->name;
                                        //echo $c;
                                        while ($my_query->have_posts()) : $my_query->the_post();
                                        if($ch==$c){
                                            //echo '<h1>'. $ch .'<h1>';
                                            ?><ul><ul><ul><li> <a href="<?php the_permalink(); ?>">
                                            <?php $scat=get_the_title();
                                            echo $scat; ?></a>
                                            <a href="<?php the_permalink(); ?>">
                                            <?php echo esc_html( get_post_meta( get_the_ID(), 'food_price', true ) ); ?> </a>
                                            </li></ul></ul></ul>
                                       <?php     
                                        }
                                        ?>
                                      <?php
                                        endwhile;
                                    } // END if have_posts loop
                                    wp_reset_query();
                                } // END foreach $tax_terms
                            } // END if $tax_terms

                }



              // echo str_repeat("-", $level) . $data[$id]["name"] . "<li>";
               display_child_nodes($id, $level + 1);
                if($level != 1){
                    echo "</li>";
                }
            }
            echo "</ul>";
        }
    }
    display_child_nodes(0, 0);

?>

This code display the post like ..

-parent1_taxonomy
 -child1_taxonomy
     -child1_taxonomy_post1
     -child1_taxonomy_post2
     -child2_taxonomy_post1
     -child2_taxonomy_post2
           -child2_taxonomy
                 -child2_taxonomy_post1
                 -child2_taxonomy_post2
 parent2_taxonomy
   -likewise...

Also the post of its sub category in root category.. Thank you in advance.

share|improve this question
    
Not to be rude, but your code is a total mess, it is totally unreadable. Please intend your code properly and add it properly in a codeblock –  Pieter Goosen Jul 30 at 11:46
    
Now its readable i believe can u solve it @Pieter –  umesh Jul 30 at 11:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.