How to avoid showing parent category if is not checked in wordpress

Question

I’m not sure if what I’m asking is possible or not. Anyways, I’m fetching custom post type from the database based on category.

When I tick subcategory or even subcategory of subcategory, all parent category gets fetched as well.

The image below shows ‘Cosmetic Aesthetic Surgery’ a subcategory of ‘Plastic Surgery’ which is also a subcategory of ‘service’

So the whole thing looks like this

Service 
    Plastic Surgery
        Cosmetic Aesthetic Surgery

When creating the post, if I tick the ‘Cosmetic Aesthetic Surgery’ ONLY, WordPress adds my post to ‘Plastic Surgery’ and ‘Service’.

Is there a way to show the only checked category, if a parent is not checked, then it gets ignored

This is my code

<?php 


class benson_doctors_plugin{

    function load_doctors( $category ){
        $args = array(
            'post_type'      => 'harley_doctors',
            'posts_per_page' => 10,
            'cat' => $category
        );

        $data = ""; 

        $loop = new WP_Query($args);
        if($loop->have_posts()){
            $data .= "<div class='doctors-speciality-holder'>";
            $data .= "<div class='doctors-speciality'>Doctors Specializing in ".$category."</div>";


            while ( $loop->have_posts() ) {
                $loop->the_post();
                $data .= "  <div class='doctors-data'>
                                <div class='doctors-post-thumbnail'>".get_the_post_thumbnail(false, 'thumbnail', NULL)."</div>
                                <div class='doctors-data-title'> 
                                ".get_the_title()."
                                </div>

                                <div class='doctors-description'>
                                  ".get_summary( get_the_content() )."
                                </div> 
                            </div>";
            }
            $data .="</div>";
        }

        // Restore original post data.
        wp_reset_postdata();


        return $data; 
    }
}




add_filter ('the_content', 'insert_doctors');
function benson_insert_doctors($content) {

    global $post; 
    $data = ""; 

    if( get_post_type( $post ) != "post" ){
        return $content; 
    }

    $categories =  get_the_category($post->ID); 

    if( is_single() ){
        if (!class_exists('benson_doctors_plugin')){
            return $content; 
        }

        $benson_doc = new benson_doctors_plugin();

        foreach ($categories as $key => $value) {
            $category = $value->cat_name;
            if( $category == "Service" || $category == "Featured" || $category == "Patient Information" ){
                // do nothing
            } else {
                $data .=$benson_doc->load_doctors( $category );
            }
        }
    }

   return $data.$content;
}
0
, , , Karue Benson Karue 3 years 2020-06-02T12:10:30-05:00 0 Answers 83 views 0

Leave an answer

Browse
Browse