所有自定义小部件不会同时显示在小部件区域中

时间:2017-09-07 作者:Prakash Pazhanisamy

我已经创建了3个名为Prakash Category Widget, Prakash Author WidgetPrakash Tag Widget. 其代码为:

<?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
?>
以上3个文件均位于plugins 文件夹激活所有3个插件后,所有自定义小部件不会同时显示在小部件区域中。它只显示小部件中的任何一个。帮助

1 个回复
最合适的回答,由SO网友:Sören Wrede 整理而成

parent::__construct(
    \'text_widget\', // This should be unique
    \'Prakash Author Widget\',
    array(\'description\' => \'A Simple Author Widget\')
);
第一个参数是小部件的基本ID,应该是小写且唯一的。所以你必须改变text_widget 到唯一字符串,例如。prakash_author_widget.

结束

相关推荐

将常用的php脚本放入插件队列

我有一个插件被wordpress拒绝了。org,其中一个原因是:对公共库的不安全要求因为您使用的是公共库,所以安全地将其排队很重要。示例:require\\u once(\'jsonld.php\');因为这是一个公共库,所以需要检测代码是否已经包含,而不是重新包含,因为如果两个人调用相同的定义和函数,这样做会导致冲突。我了解jQuery和CSS排队,但这是一个具有多个函数的PHP脚本。如果这个脚本很常见,我需要在WP核心中引用它吗</否则,我只需要查看我正在调用的函数并将它们封装在一个类中(我对此