Make WordPress Core

Opened 6 months ago

Closed 6 months ago

Last modified 6 months ago

#61031 closed feature request

Taxonomy List Widget

Reported by: nkpathan's profile nkpathan Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Widgets Keywords:
Focuses: Cc:

Description

We need to create feature that provides lists of Taxonamy Terms as Widget.

Change History (3)

#1 @nkpathan
6 months ago

  • Resolution set to worksforme
  • Status changed from new to closed

#2 @nkpathan
6 months ago

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' );
Last edited 6 months ago by sabernhardt (previous) (diff)

#3 @sabernhardt
6 months ago

  • Milestone Awaiting Review deleted
  • Version trunk deleted

Thanks for the follow-up!

Note: See TracTickets for help on using tickets.