自定义默认类别小组件

时间:2012-07-14 作者:Jamie

我正试图从wordpress中破解默认类别小部件。我有我想要的方式显示类别,但我在编辑输出时遇到了问题。小部件显示了我想要的每个帖子中的帖子数量,但它也在括号中显示了数字。我不想那样。我知道如何摆脱它们,但我似乎无法使其发挥作用。我试过了

$cat_args[\'title_li\'] = \'\';
$list = wp_list_categories(apply_filters(\'widget_categories_args\', $cat_args));
$list .= str_replace(\'(\', \'<span class="number">\', $list);
$list .= str_replace(\')\', \'<span class="number">\', $list);
echo $list;
它只是以同样的方式继续显示类别。我不明白为什么它没有响应列表。我正在使用默认窗口小部件中的类别代码。wp includes文件夹中的php文件。

2 个回复
最合适的回答,由SO网友:Milo 整理而成

wp_list_categories 默认情况下,您的str_replace 由于未分配任何内容,因此无法工作$list.

SO网友:Dave Romsey

我也遇到过这种情况,最终复制了内置的列表类别小部件,并对其进行了定制以满足我的需要。我在代码中留下了注释掉的$replacement regex的几个变体。

<?php

// Register our tweaked Category Archives widget
function myprefix_widgets_init() {
  register_widget( \'WP_Widget_Categories_custom\' );
}
add_action( \'widgets_init\', \'myprefix_widgets_init\' );


/**
 * Duplicated and tweaked WP core Categories widget class
 */
class WP_Widget_Categories_Custom extends WP_Widget {

  function __construct() {
    $widget_ops = array( \'classname\' => \'widget_categories widget_categories_custom\', \'description\' => __( "A list of categories, with slightly tweaked output.", \'mytextdomain\'  ) );
    parent::__construct( \'categories_custom\', __( \'Categories Custom\', \'mytextdomain\' ), $widget_ops );
  }

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

    $title = apply_filters( \'widget_title\', empty( $instance[\'title\'] ) ? __( \'Categories Custom\', \'mytextdomain\'  ) : $instance[\'title\'], $instance, $this->id_base);

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

    <ul>
    <?php
      // Get the category list, and tweak the output of the markup.
      $pattern = \'#<li([^>]*)><a([^>]*)>(.*?)<\\/a>\\s*\\(([0-9]*)\\)\\s*<\\/li>#i\';  // removed ( and )

      // $replacement = \'<li$1><a$2>$3</a><span class="cat-count">$4</span>\'; // no link on span
      // $replacement = \'<li$1><a$2>$3</a><span class="cat-count"><a$2>$4</a></span>\'; // wrap link in span
      $replacement = \'<li$1><a$2><span class="cat-name">$3</span><span class="cat-count">$4</span></a>\'; // give cat name and count a span, wrap it all in a link

      $subject      = wp_list_categories( \'echo=0&orderby=name&exclude=&title_li=&depth=1&show_count=1\' );    
      echo preg_replace( $pattern, $replacement, $subject );
    ?>
    </ul>
    <?php
    echo $after_widget;
  }

  function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    $instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
    $instance[\'count\'] = 1;
    $instance[\'hierarchical\'] = 0;
    $instance[\'dropdown\'] = 0;

    return $instance;
  }

  function form( $instance ) {
    //Defaults
    $instance = wp_parse_args( (array) $instance, array( \'title\' => \'\') );
    $title = esc_attr( $instance[\'title\'] );
    $count = true;
    $hierarchical = false;
    $dropdown = false;
?>
    <p><label for="<?php echo $this->get_field_id(\'title\', \'mytextdomain\' ); ?>"><?php _e( \'Title:\', \'mytextdomain\'  ); ?></label>
    <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>

    <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id(\'count\'); ?>" name="<?php echo $this->get_field_name(\'count\'); ?>"<?php checked( $count ); ?> disabled="disabled" />
    <label for="<?php echo $this->get_field_id(\'count\'); ?>"><?php _e( \'Show post counts\', \'mytextdomain\'  ); ?></label><br />
<?php
  }

}

结束