[wp-trac] [WordPress Trac] #61031: Taxonomy List Widget

WordPress Trac noreply at wordpress.org
Mon Apr 22 06:05:23 UTC 2024


#61031: Taxonomy List Widget
-----------------------------+------------------------------
 Reporter:  nkpathan         |       Owner:  (none)
     Type:  feature request  |      Status:  closed
 Priority:  normal           |   Milestone:  Awaiting Review
Component:  Widgets          |     Version:  trunk
 Severity:  normal           |  Resolution:  worksforme
 Keywords:                   |     Focuses:
-----------------------------+------------------------------

Comment (by nkpathan):

 Solution :-
 =========================


 class Custom_Taxonomy_Widget extends WP_Widget {

     // Constructor
     public function __construct() {
         parent::__construct(
             'custom_taxonomy_widget', // Base ID
             'Custom Taxonomy Widget', // Name
             array( 'description' => __( 'A widget to display custom
 taxonomy terms', 'text_domain' ), ) // Args
         );
     }

     // Widget Form
     public function form( $instance ) {
         // Output the widget form fields
         $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
         $taxonomy = ! empty( $instance['taxonomy'] ) ?
 $instance['taxonomy'] : '';
         ?>
         <p>
             <label for="<?php echo $this->get_field_id( 'title' );
 ?>"><?php _e( 'Title:' ); ?></label>
             <input class="widefat" id="<?php echo $this->get_field_id(
 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>"
 type="text" value="<?php echo esc_attr( $title ); ?>">
         </p>
         <p>
             <label for="<?php echo $this->get_field_id( 'taxonomy' );
 ?>"><?php _e( 'Taxonomy:' ); ?></label>
             <select class="widefat" id="<?php echo $this->get_field_id(
 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' );
 ?>">
                 <?php
                 // Get list of taxonomies
                 $taxonomies = get_taxonomies( array( 'public' => true ),
 'objects' );
                 foreach ( $taxonomies as $taxonomy_obj ) {
                     echo '<option value="' . $taxonomy_obj->name . '" ' .
 selected( $taxonomy, $taxonomy_obj->name, false ) . '>' .
 $taxonomy_obj->label . '</option>';
                 }
                 ?>
             </select>
         </p>
         <?php
     }

     // Update Widget
     public function update( $new_instance, $old_instance ) {
         $instance = array();
         $instance['title'] = ( ! empty( $new_instance['title'] ) ) ?
 strip_tags( $new_instance['title'] ) : '';
         $instance['taxonomy'] = ( ! empty( $new_instance['taxonomy'] ) ) ?
 $new_instance['taxonomy'] : '';
         return $instance;
     }

     // Widget Display
     public function widget( $args, $instance ) {
         // Widget output
         $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
         $taxonomy = ! empty( $instance['taxonomy'] ) ?
 $instance['taxonomy'] : '';

         echo $args['before_widget'];
         if ( ! empty( $title ) ) {
             echo $args['before_title'] . apply_filters( 'widget_title',
 $title ) . $args['after_title'];
         }

         if ( ! empty( $taxonomy ) ) {
             $terms = get_terms( $taxonomy );

             if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
                 echo '<ul>';
                 foreach ( $terms as $term ) {
                     echo '<li><a href="' . get_term_link( $term ) . '">' .
 $term->name . '</a></li>';
                 }
                 echo '</ul>';
             } else {
                 echo '<p>No terms found.</p>';
             }
         }

         echo $args['after_widget'];
     }
 }

 // Register Custom Taxonomy Widget
 function register_custom_taxonomy_widget() {
     register_widget( 'Custom_Taxonomy_Widget' );
 }
 add_action( 'widgets_init', 'register_custom_taxonomy_widget' );

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/61031#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list