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.

With this code parent term page lists all the child terms and the child term page lists all of the posts in current term, and the single page lists a big flat list of terms(hierarchical).

Now,

The parent term page is fine.

When it's child term page, i want to list all child terms( not showing with this code ) + the posts in current term( showing ).

And when it's single post, i want to list the same as child term page.

        $term = get_queried_object();
        $tax = 'ntp_package_type';
        $parents = $term->parent;
        $term_id = $term->term_id;

        if($parents == 0){
            wp_list_categories( array (
                            'taxonomy'  => 'ntp_package_type',
                            'pad_counts'=> 0,
                            'title_li'  => '',
                            'child_of'  => $term_id,
                            )
            );
        }
        elseif (is_tax($tax, $term->name)){

            $args = array(
                    'post_type' => 'ntp_package',
                    $tax => $term->name,
            );
            $wp_query = new WP_Query($args);
            if( $wp_query->have_posts() ):
            while ( $wp_query->have_posts() ) : $wp_query->the_post();
            echo "<p><a href=\"".get_permalink()."\">".$post->post_title."</a></p>";
            endwhile;
            endif;

        }
        elseif (is_single()){

        }

It is for a sidebar sub nav. is it a right way to do? I'd really appreciate any help.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

ok, i don't know how this looks but i've got exactly what i wanted.

<?php
        $term = get_queried_object();
        $tax = 'ntp_package_type';
        $parents = $term->parent;
        $term_id = $term->term_id;

        if($parents == 0 && !is_single()){
        wp_list_categories( array (
                            'taxonomy'  => 'ntp_package_type',
                            'pad_counts'=> 0,
                            'title_li'  => '',
                            'child_of'  => $term_id,
                            )
            );  
        }

        elseif ($parents > 0 && is_tax($tax, $tax->name)){

            $args = array(
                    'post_type' => 'ntp_package',
                    $tax => $term->name,
            );
            echo "<h2>".$term->name."</h2>";

            $wp_query = new WP_Query($args);
            if( $wp_query->have_posts() ):
            while ( $wp_query->have_posts() ) : $wp_query->the_post(); 

            echo "<p><a href=\"".get_permalink()."\">".$post->post_title."</a></p>";
            endwhile;
            endif;
            wp_reset_postdata();

            wp_list_categories( array (
                'taxonomy'  => 'ntp_package_type',
                'pad_counts'=> 0,
                'title_li'  => '',
                'child_of'  => $term->parent,
                'exclude'   => $term_id,
                )
            );
        }

        elseif(is_single()){

        $single = get_queried_object();
        $sing_id = $single->ID;

        $singterm = wp_get_post_terms($sing_id, $tax);
        $singterm_slug = $singterm[0]->slug;
        $singterm_parent = $singterm[0]->parent;
        $singterm_id = $singterm[0]->term_id;

        $args = array(
                'taxonomy'  => $tax,
                'term'      => $singterm_slug,
        );
        ?>

        <h2>
        <a href="<?php echo get_term_link($singterm_slug, $tax); ?>"><?php echo $singterm[0]->name; ?> </a>
        </h2>

        <?php
        $singquery = new WP_Query($args);
        while ($singquery->have_posts()) : $singquery->the_post();
        ?>

        <p>
        <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
        </p>

        <?php
        endwhile;
        wp_reset_postdata();


        wp_list_categories( array (
            'taxonomy'  => 'ntp_package_type',
            'pad_counts'=> 0,
            'title_li'  => '',
            'child_of'  => $singterm_parent,
            'exclude'   => $singterm_id,
            )
        );  

        }


        ?>
share|improve this answer

Your Answer

 
discard

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

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