所有的定制小部件是不是在小工具区域显示在我创建了一个名为3个的自定义部件的同时
问题描述:
普拉卡什类别的Widget,普拉卡什作者的Widget,普拉卡什标签的Widget。他们的代码是:所有的定制小部件是不是在小工具区域显示在我创建了一个名为3个的自定义部件的同时
<?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个文件都位于插件文件夹。激活所有3个插件后,所有自定义小部件不会同时显示在小部件区域中。它只显示任何一个小部件。帮帮我!
答
parent::__construct(
'text_widget', // This should be unique
'Prakash Author Widget',
array('description' => 'A Simple Author Widget')
);
第一个参数是小部件的基本ID,应该是小写字母和唯一的。所以你必须改变text_widget
为唯一的字符串,例如prakash_author_widget
。