如何从标签档案中删除特定类别?

时间:2015-02-09 作者:TMTMEE

我需要标签档案不显示特定类别。谢谢

1 个回复
SO网友:junkrig

一种方法是创建一个自定义小部件,它使用wp\\u list\\u categories()显示类别。然后使用wp\\u list\\u categories()的排除选项排除您不想显示的任何类别:http://codex.wordpress.org/Template_Tags/wp_list_categories#Include_or_Exclude_Categories.

下面的示例在外观/小部件中创建一个自定义类别小部件。此代码可以添加到函数中。php或在插件中使用。

/*-----------------------------------------------------------------------------------*/
/* Create category list widget */
/*-----------------------------------------------------------------------------------*/
class ABCD_Category_Widget extends WP_Widget
{
  function ABCD_Category_Widget()
  {
    $options = array( \'classname\' => \'ABCD_Category_Widget\', \'description\' => \'Displays a list of categories\' );
    $this->WP_Widget( \'ABCD_Category_Widget\', \'ABCD Category List\', $options );
  }

  function form( $instance )
  {
    $instance = wp_parse_args( ( array ) $instance, array( \'title\' => \'\' ) );
    $title = $instance[\'title\'];
  ?>
  <p><label for="<?php echo $this->get_field_id( \'title\' ); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo attribute_escape( $title ); ?>" /></label></p>
  <?php
  }

  function update( $new_instance, $old_instance )
  {
    $instance = $old_instance;
    $instance[\'title\'] = $new_instance[\'title\'];
    return $instance;
  }

  function widget( $args, $instance )
  {
    extract( $args, EXTR_SKIP );

    echo $before_widget;
    $title = empty( $instance[\'title\'] ) ? \' \' : apply_filters( \'widget_title\', $instance[\'title\'] );

    if ( !empty( $title ) )
      echo $before_title . $title . $after_title;

    $args = array(
    \'orderby\'       => \'name\', 
    \'order\'         => \'ASC\', 
    \'number\'        => null,
    \'optioncount\'   => true, 
    \'exclude_admin\' => true, 
    \'show_fullname\' => false,
    \'hide_empty\'    => true,
    \'echo\'          => true,
    \'style\'         => \'list\',
    \'exclude\'       => array( 20, 26 ), // IDs of categories you want to exclude
    \'title_li\'      => __( \'\' ),
    \'html\'          => true
    );


    echo \'<ul>\', wp_list_categories( $args ), \'</ul>\';

    echo $after_widget;
  }

}
add_action( \'widgets_init\', create_function( \'\', \'return register_widget("ABCD_Category_Widget");\' ) );

结束