Quantcast
Channel: WordPress.org Forums » All Posts
Viewing all articles
Browse latest Browse all 134700

panjids on "[how to] make child category post listing not display in parent category"

$
0
0

hello guys, i want to ask about category post listing (title and permalink to the post.
i made a custom post type including its taxonomy. i want to list all post based on category (only title and its permalink) in the sidebar.
everything just went well exactly like I expect.

1. category ABC
--- a. post x
--- b. post y
2. category CDE
--- a. post z
--- b. post v

it goes well when the category has no child in it.
the problem arises when I start making child categories and make post in it.
now post that belongs to the child category also appears in parent category. just like this:
(i created a new post of j, k and child category 1.1 childcat ofABC and 1.1.1 childcat ofchildcatofABC)

1. category ABC
--- a. post x
--- b. post y
--- c. post j <--- post of 1.1 childcat ofABC
--- d. post k <--- post of 1.1.1 childcat ofchildcatofABC
--- 1.1 childcat ofABC (new child category)
------ a. post j
------ b. post k <--- post of 1.1.1 childcat ofchildcatofABC
------ 1.1.1 childcat ofchildcatofABC (new child category)
--------- a. post k
2. category CDE
--- a. post z
--- b. post v

how to make there is no child category post appears in its parent category ?
thanks before.

here is my query to list post:

function kb_sections( $sections = array() ) { // by default $sections is an empty array
    // tell the function your custom taxonomy name
    $taxonomy = 'section';

    // set link class (for determing later if the term is a top-level term)
    $link_class = '';

    /*
    *  This IF statement checks if the $sections array is empty.
    *  If it is empty, then populate it with the top-level taxonomy
    *  values. Then begin an unordered list in which to place the terms
    */
    if ( empty( $sections ) ) {
        $link_class = 'root';  // set the link class to root on the first iteration

        $sections = get_terms( $taxonomy, array(
            'parent' => 0, // only return top-level terms
            'hide_empty' => 0 // don't return empty terms
        ) ); // (taxonomy, args)
        echo '<ul id="kb-sections" class="unstyled">'; // start our unordered list
    }

    /*
    *  Iterate through each item in the $sections array and output
    *  the term into the list. Then create a sub-menu of each term
    *  that either creates the next level of terms or runs a query
    *  that outputs all posts that are tagged with that term
    */
    foreach ( $sections as $section ) {  // iterate through each taxonomy term in the array

        $section_children = get_terms( $taxonomy, array( // get terms of children
            'parent' => $section->term_id, // identify children if the parent is in the $sections array
            'hide_empty' => 0 // again, hide empties
        ) );

        echo '<li>'; // create a new list item in the unordered list

        echo '<span>' . $section->name . '</span>'; // display the taxonomy name

        if ( !empty( $section_children ) ) { // if there are children in the array

            echo '<ul id="' . $section->slug . '" class="children">'; // create a new nested unordered list with the ID of the parent slug
			$current_cat = intval( get_query_var('cat') );
			$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
			$newargs = array( // arguments for the query to populate lowest menu with items
                'post_type'=> 'knowledgebase',
                'section'    => $section->name,
                'order'    => 'ASC',
                'posts_per_page' => -1
            );
            $my_new_query = new WP_Query( $newargs ); // run query to find all posts using argument above
            echo '<ul>'; //create the final unordered list, one that stores links to titles
            while ( $my_new_query->have_posts() ) : $my_new_query->the_post(); // start the custom loop
                // this must be output in 5 lines, and not all in one, or else errors are thrown.
                echo '<li><a href="';
                echo the_permalink();
                echo '">';
                echo the_title();
                echo '</a></li>';
            endwhile;
            wp_reset_postdata();
            echo '</ul>';

            kb_sections( $section_children ); // call again the kb_sections which looks for sub-sub-menus
        }

        if ( empty( $section_children ) ) { // however, if there are no children in the array (aka, we've arrived at the final level)

            /*
            *  The following query looks for all posts that are in
            *  the section (custom taxonomy) for the custom post
            *  type (knowledgebase). Also, keep in mind, posts_per_page
            *  must be set to -1 to show all posts. Otherwise it respects
            *  the default WordPress settings (whatever you've set in Settings > Reading)
            */
            $newargs = array( // arguments for the query to populate lowest menu with items
                'post_type'=> 'knowledgebase',
                'section'    => $section->name,
                'order'    => 'ASC',
                'posts_per_page' => -1
            );
            $my_new_query = new WP_Query( $newargs ); // run query to find all posts using argument above
            echo '<ul>'; //create the final unordered list, one that stores links to titles
            while ( $my_new_query->have_posts() ) : $my_new_query->the_post(); // start the custom loop
                // this must be output in 5 lines, and not all in one, or else errors are thrown.
                echo '<li><a href="';
                echo the_permalink();
                echo '">';
                echo the_title();
                echo '</a></li>';
            endwhile;
            wp_reset_postdata();
            echo '</ul>';
        }
        echo "</li>";
    }
    echo "</ul>";
}

--
i modified code a little (just copy some query to other section) which i got from here and here.


Viewing all articles
Browse latest Browse all 134700

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>