0

How to only show parent categories in sidebar?

But also when browsing posts, to show all the subcategories of the parent category the current post is in, showing the hierarchy. the other parent categories will also be displayed, but only the one that the post is in will reveal the subcategories.

So basically show the subcategories of a parent category only when viewing a post in that parent category.

Example:

on single.php 
for example, if there are 3 parent categories, with subcategories:

Fruits (parent)
 Apples
 Plums
Veggies (parent)
 Tomatoes
 Onions
Nuts (parent)
 Peanuts
 Almonds

if the post is in subcategory Apples, the sibear should show:

Fruits 
 Apples
 Plums
Veggies 
Nuts 

if the post is in subcategory Almonds, the sibear should show:

Fruits
Veggies
Nuts
 Peanuts
 Almonds

on anything else (index, category pages) sidebar should only show parent categories, no subcategory.
1

You can create a custom widget for that.

// Widget Class ==============================
class WPSE154979_Widget extends WP_Widget
{
    function WPSE154979_Widget()
    {
        $widget_ops = array(
            'classname'     => 'WPSE154979_custom_widget',
            'description'   => __('Post Category Children\'s or Parent Categories')
        );
        $control_ops = array( 'width' => 200, 'height' => 400);
        $this->WP_Widget( 'WPSE154979_custom', 'Custom Categories', $widget_ops, $control_ops );
        $this->alt_option_name = 'WPSE154979_custom';
    }


    function widget( $args, $instance)
    {
        extract( $args);
        $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );

        echo $before_widget;
        if( $title )
            echo $before_title . $title . $after_title;

        // on single post page
        if( is_single() && is_object_in_taxonomy( get_post_type(), 'category' ) )
        {
            $cats = wp_get_object_terms( 
                get_the_ID(), 
                'category', 
                array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC',  ) 
            );
            $parent_id = array_shift( $cats );
            wp_list_categories( 'title_li=&show_option_none=&hide_empty=0&parent='. $parent_id );
        }

        // on category page
        elseif( is_category() )
        {
            $parent_id = (int) get_query_var('cat');
            wp_list_categories( 'title_li=&show_option_none=&hide_empty=0&parent='. $parent_id );
        }

        // on others page
        else
        {
            $parent_id = 0;
            wp_list_categories( 'title_li=&show_option_none=&hide_empty=0&parent='. $parent_id );
        }

        echo $after_widget;
    }
    function update( $new_instance, $old_instance ) {
        $instance                   = $old_instance;
        $instance['title']          = strip_tags( $new_instance['title']);
        return $instance;
    }
    function form( $instance )
    {
        $title = isset($instance['title']) ? esc_attr($instance['title']) : ''; ?><p>
            <strong><?php _e( 'Title:' ); ?></strong>
            <br /><input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" 
            value="<?php echo $title; ?>" />
        </p><?php
    }
}


// Register Widget ==============================
add_action('widgets_init', 'WPSE154979_Widget_Init');
function WPSE154979_Widget_Init(){
    register_widget( 'WPSE154979_Widget' );
}

If you add the above code to you themes functions.php file, you would see a new available widget on your WP Admin -> Widgets page. Drag and Drop it to a proper place.

  • great! just that it shows all the subcategories when viewing a post, not only the subcategories of the parent category that the post belongs to. – whitelord Jul 17 '14 at 23:39
  • it is working for parent categories, but it should do that for a post, not for the parent category. – whitelord Jul 18 '14 at 0:01
  • now it shows no categories in the sidebar, on a post page. – whitelord Jul 18 '14 at 0:06
  • Updated. If the current post category has no direct children, it should show nothing. Same for the category page. – Shazzad Jul 18 '14 at 0:08
  • the posts are in subcategories, so it won't work. – whitelord Jul 18 '14 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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