我已经创建了3个名为Prakash Category Widget, Prakash Author Widget 和Prakash 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个插件后,所有自定义小部件不会同时显示在小部件区域中。它只显示小部件中的任何一个。帮助
最合适的回答,由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
.