2017-09-07 4 views
-1

J'ai créé 3 widgets personnalisés nommés Prakash Catégorie Widget, Prakash Auteur Widget, Prakash Tag Widget. Leurs codes sont les suivants:Tous les widgets personnalisés ne sont pas visibles dans la zone widget en même temps

<?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 
?> 

Tous les 3 fichiers ci-dessus sont situés dans dossier plugins. Après avoir activé tous les 3 plugins, tous les widgets personnalisés ne sont pas affichés dans la zone du widget en même temps. Il montre seulement l'un des widgets. Aidez-moi!

Répondre

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

Le premier paramètre est l'ID de base du widget et doit être en minuscules et unique. Vous devez donc remplacer text_widget par des chaînes uniques, par ex. prakash_author_widget.