用帖子编号显示类别的小部件

时间:2014-04-25 作者:pmor

是否有任何解决方案可以在不改变字体大小的情况下显示带有帖子编号(如“存档”小部件)的类别?

UPD0:如果我的问题不清楚,很抱歉。我想要什么?我们有一个非常好的小部件,它生成如下存档:

enter image description here

问题是,我们如何才能获得相同的类别视图?像这样:

Categories
My Category (12)
Another category (16)

2 个回复
SO网友:Pieter Goosen

这里不涉及编码。Wordpress已经有一个用于此功能的内置小部件。如果您转到“Widgets”屏幕。您将看到有一个名为Categories的小部件

Screenshot1

您可以将其拖放到需要显示小部件的侧栏中。完成此操作后,小部件将打开,从那里您可以勾选“显示帖子数量”框以显示类别的帖子数量。

Screenshot2

SO网友:Shakalya Uttam
class Shaka_Categories_Widget extends WP_Widget{
    function __construct() {
        parent::__construct(
            \'shaka_categories_widget\', // Base ID
            \'Shaka Categories\', // Name
            array(\'description\' => __( \'Displays post Categories with counts\'))
           );
    }
    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance[\'title\'] = strip_tags($new_instance[\'title\']);
        //$instance[\'post_type\'] = strip_tags($new_instance[\'post_type\']);
        //$instance[\'numberOfListings\'] = strip_tags($new_instance[\'numberOfListings\']);
        return $instance;
    }
    function form($instance) {
        if( $instance) {
            $title = esc_attr($instance[\'title\']);
        } else {
            $title = \'\';
        }
        ?>
            <p>
            <label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Title\', \'sim_most_viewed\'); ?></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>

        <?php
    }
    function widget($args, $instance) {
        extract( $args );
        $title = apply_filters(\'widget_title\', $instance[\'title\']);

        echo $before_widget;
        if ( $title ) {
            echo $before_title . $title . $after_title;
        }
        $this->getMostViewedListings();
        echo $after_widget;
    }
    function getMostViewedListings() { //html
        $args = array(
        \'type\' => \'post\'
        );
        $categories = get_categories($args);
        //echo "<pre>";var_dump($categories);echo "</pre>";
        if($categories){
            echo "<ul>";
            foreach($categories as $cat){
                echo "<li><a href=\'".site_url().\'/category/\'.$cat->slug."\'  class=\'cat-item cat-item-".$cat->term_id."\'>".$cat->name." <span class=\'post-count\'>".$cat->count."</span></a></li>";
            }   
            echo "</ul>";
        }
    }


} //end class

register_widget(\'Shaka_Categories_Widget\');
结束

相关推荐

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\' ); 但问