0

Is there any solution to show categories with posts numbers like 'Archives' widget without changing the font size?

UPD0: Sorry if my question is not clear. What I want? We have a pretty nice widget, which generates the following Archives like this:

enter image description here

The question is, how we can get the same view for categories? Like this:

Categories
My Category (12)
Another category (16)
  • 1
    What have you tried so far. Please add the code that you have so far, working or not. – Pieter Goosen Apr 25 '14 at 17:03
1

There is no coding involved here. Wordpress has already a build in widget for this function. If you go to the "Widgets" screen. you'll see that there are a widget called Categories

Screenshot1

You can drag and drop that to your sidebar where you need to display the widget. When you've done that, the widget will open, from there you can tick the 'Show post counts' box to display the post count of the categories.

Screenshot2

| improve this answer | |
1
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');
| improve this answer | |

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.