0

I have created 3 custom widgets named Prakash Category Widget, Prakash Author Widget and Prakash Tag Widget. Their codes are:

<?php
// Begin Prakash Category Widget
/*
Plugin Name: Prakash Category Widget
Description: A Simple Category Widget
Version: 1.0
Author: Prakash Pazhanisamy
Author URI: http://prakashpazhanisamy.wordpress.com
*/

class CategoryWidget extends WP_Widget
{
    public function __construct() {
        parent::__construct("text_widget", "Prakash Category Widget",
            array("description" => "A Simple Category Widget"));
    }

    public function widget($args, $instance) {
        $cats = get_categories();  // Get categories

        if ($cats) :
        echo '<h2>Categories</h2>';
        echo '<ul>';
        foreach ($cats as $cat) { 

            // Create a query for the category to determine the number of posts
            $category_id= $cat->term_id;

            $cat_query = new WP_Query( array( 'post_type' => 'post', 
                        'posts_per_page' => -1,
                        'cat' => $category_id
            ) );
            $count = $cat_query->found_posts;

            // Print each category with it's count as a list item
            echo "<li>" . $cat->name . " (" . $count . ")</li>";

        }

        wp_reset_query();  // Restore global post data 
        echo '</ul>';
        endif;
    }
}
add_action("widgets_init", function () { register_widget("CategoryWidget"); });
// End Prakash Category Widget
?>

<?php
//Begin Prakash Author Widget
/*
Plugin Name: Prakash Author Widget
Description: A Simple Author Widget
Version: 1.0
Author: Prakash Pazhanisamy
Author URI: http://prakashpazhanisamy.wordpress.com
*/

class AuthorWidget extends WP_Widget
{
    public function __construct() {
        parent::__construct("text_widget", "Prakash Author Widget",
            array("description" => "A Simple Author Widget"));
    }

    public function widget($args, $instance) {
        global $wpdb;

        $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users ORDER BY display_name");
        echo "<h2>Authors</h2>";
        foreach($authors as $author) {
            echo "<li>";
            echo "<a href=\"".get_bloginfo('url')."/?author=";
            echo $author->ID;
            echo "\">";
            echo get_avatar($author->ID);
            echo "</a>";
            echo '<div>';
            echo "<a href=\"".get_bloginfo('url')."/?author=";
            echo $author->ID;
            echo "\">";
            the_author_meta('display_name', $author->ID);
            echo "</a>";
            echo "</div>";
            echo "</li>";
        }
    }
}
add_action("widgets_init", function () { register_widget("AuthorWidget"); });
//End Prakash Author Widget
?>

<?php
//Begin Prakash Tag Widget
/*
Plugin Name: Prakash Tag Widget
Description: A Simple Tag Widget
Version: 1.0
Author: Prakash Pazhanisamy
Author URI: http://prakashpazhanisamy.wordpress.com
*/

class TagWidget extends WP_Widget
{
    public function __construct() {
        parent::__construct("text_widget", "Prakash Tag Widget",
            array("description" => "A Simple Tag Widget"));
    }

    public function widget($args, $instance) {
        $terms = get_terms('post_tag',array('hide_empty'=>false));
        echo "<h2>Tags</h2>";
        foreach($terms as $t) {
          echo $t->name.' ('.$t->count.')</br>';
        }
    }
}
add_action("widgets_init", function () { register_widget("TagWidget"); });
//End Prakash Tag Widget
?>

All the above 3 files are located in plugins folder. After activating all the 3 plugins, all custom widgets are not showing in widget area at the same time. It is showing only any one of the widget. Help!

1 Answer 1

1
parent::__construct(
    'text_widget', // This should be unique
    'Prakash Author Widget',
    array('description' => 'A Simple Author Widget')
);

The first parameter is the Base ID for the widget and should be lowercase and unique. So you have to change text_widget to unique strings, e.g. prakash_author_widget.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.