Sub Categories in Q&A

I have enabled 36 categories for the Questions. Most of the categories have multiple sub categories as well. How can I enable subcategories from a selected category to be shown as a "widget".

For example, when a user selects the category "Automotive Services", I would love to have a widget on the right side which shows the sub categories under "Automotive Services"

  • Kimberly

    Hi there!

    This lists all child categories of the current post's category:

    <?php
        echo '<ul>';
        $post_child_cat = array();
        foreach((get_the_category()) as $cat) {
            $args = array( 'child_of' => $cat->cat_ID );
            $categories = get_categories( $args );
            if( $categories ) foreach( $categories as $category ) {
                echo '<li class="cat-item cat-item-'.$category->term_id.'">'.
                '<a title="'.$category->description.'" href="';
                echo bloginfo('url');
                echo '/category/'.$cat->slug.'/'.$category->slug.'">'.
                $category->name.'</a></li>';
            }
        }
       echo '</ul>';
    ?>

    You will need to adjust to allow for the Q&A category references :slight_smile:

    Best,

    Kimberly


    If the above action did not result in the resolution or common end to this ticket then please feel free to come back here for comment. Just be sure you mark "Not Resolved" to re-open or we might miss it! :slight_smile: Thanks!

  • Hakan

    Hi Jamy,

    This is quite tricky and much more complex then it seems, but it is still possible.

    For future compatibility with the plugin update please proceed as follows:

    Find the following codes around line 262 of wp-content/plugins/qa/core/widgets.php:

    $cat_args = array(
    			'taxonomy' => 'question_category',
    			'orderby' => 'name',
    			'hierarchical' => true,
    			'show_count' => $instance['count'],
    			'title_li' => ''
    		);

    Just after those, add the following line:

    $cat_args = apply_filters( 'qa_category_widget_args', $cat_args );

    Now, in functions.php of your theme, add these:

    function qa_widget_show_sub_cats( $cat_args ) {
    	global $post;
    	$cats = get_the_terms( $post->ID, 'question_category' );
    	foreach ( $cats as $category ) {
    		if ( $category->term_id ) {
    			$termchildren = get_term_children( $category->term_id, 'question_category' );
    			if ( $termchildren && !empty( $termchildren ) && !is_object( $termchildren )  ) {
    				$cat_args['child_of'] = $category->term_id;
    				break;
    			}
    		}
    	}
    	return $cat_args;
    }
    add_filter( 'qa_category_widget_args', 'qa_widget_show_sub_cats' );

    It is done.

    This modification:
    1. Shows full category list (including the sub categories) if question's category does not have sub categories
    2. Shows only sub categories list if there are some
    3. will work even after the update of the plugin

    Cheers,
    Hakan

  • Hakan

    Hi,

    Well, yes, my solution was for the Question pages, not for question category pages. To generalize it, just change it as follows:

    function qa_widget_show_sub_cats( $cat_args, $instance ) {
    	global $post;
    	if ( is_qa_page() ) {
    		$cats = get_the_terms( $post->ID, 'question_category' );
    		foreach ( $cats as $category ) {
    			if ( $category->term_id ) {
    				$termchildren = get_term_children( $category->term_id, 'question_category' );
    				if ( $termchildren && !empty( $termchildren ) && !is_object( $termchildren )  ) {
    					$cat_args['child_of'] = $category->term_id;
    					break;
    				}
    			}
    		}
    	}
    	return $cat_args;
    }
    add_filter( 'qa_category_widget_args', 'qa_widget_show_sub_cats', 10, 2 );

    Cheers,
    Hakan

  • jamy

    Hakan,

    This doesn't work either. See, my requirements is as follows:

    For example, there is a question category Automotive Services

    It has teh following sub categories:

    — Car Care
    — Car Cleaning and Polishing
    — Car Rental
    — Car Second Hand Sales
    — Drivers
    — Driving Services

    So when users go to the page

    http://nicholla.prolabz.com/questions/categories/automotive-services/

    I want the question category widget to show only the categories and sub categories relates to "Automotive Services"

    I have mailed you my credentials earlier. Please help me sort this out. I have a submission tomorrow.

    Best regards

  • Hakan

    Hi jamy,

    I checked it again and it works fine for me with Twenty Eleven theme. Please see attached screenshots, including the main question, category and sub-category pages.

    Please note that Q&A does not natively supports this and if this solution does not work with your theme and/or current usage of your widgets, then you may want to consider a dedicated support:
    https://premium.wpmudev.org/wp-jobs/

    Cheers,
    Hakan

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.