如何在侧边栏中仅显示父类别

时间:2014-07-17 作者:vyperlook

如何在侧边栏中仅显示父类别?

但在浏览帖子时,也要显示当前帖子所在父类别的所有子类别,显示层次结构。其他父类别也将显示,但只有帖子所在的父类别才会显示子类别。

因此,基本上只在查看父类别中的帖子时才显示父类别的子类别。

示例:

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 个回复
SO网友:Shazzad

您可以为此创建一个自定义小部件。

// 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\' );
}
如果将上述代码添加到主题函数中。php文件,您将在WP Admin -> Widgets 页将其拖放到适当的位置。

结束

相关推荐

Show Pages in Categories

通过将此代码添加到函数中,我创建了category函数。php:function page_category() { register_taxonomy_for_object_type(\'category\', \'page\'); } // Add to the admin_init hook of your theme functions.php file add_action( \'init\', \'page_category\' ); 但问