Make WordPress Core

Opened 2 months ago

Closed 8 weeks ago

Last modified 7 weeks 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
8 weeks ago

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

#2 @nkpathan
8 weeks 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( $instancetitle? ) ? $instancetitle? : ;
$taxonomy = ! empty( $instancetaxonomy? ) ? $instancetaxonomy? :
;
?>
<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();
$instancetitle? = ( ! empty( $new_instancetitle? ) ) ? strip_tags( $new_instancetitle? ) : ;
$instancetaxonomy? = ( ! empty( $new_instancetaxonomy? ) ) ? $new_instancetaxonomy? :
;
return $instance;

}

Widget Display
public function widget( $args, $instance ) {

Widget output
$title = ! empty( $instancetitle? ) ? $instancetitle? : ;
$taxonomy = ! empty( $instancetaxonomy? ) ? $instancetaxonomy? :
;

echo $argsbefore_widget?;
if ( ! empty( $title ) ) {

echo $argsbefore_title? . apply_filters( 'widget_title', $title ) . $argsafter_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 $argsafter_widget?;

}

}

Register Custom Taxonomy Widget
function register_custom_taxonomy_widget() {

register_widget( 'Custom_Taxonomy_Widget' );

}
add_action( 'widgets_init', 'register_custom_taxonomy_widget' );

Version 0, edited 8 weeks ago by nkpathan (next)

#3 @sabernhardt
8 weeks ago

  • Milestone Awaiting Review deleted
  • Version trunk deleted

Thanks for the follow-up!

Note: See TracTickets for help on using tickets.