List a current posts’ taxonomy terms in a widget in WordPress

I would very much like to know if there is a widget that exists that would let me show the current page’s associated taxonomy terms (preferably hierarchically) in a widget.

For example, if the current post has a Taxonomy term “Tom Hanks” in the Taxonomy of “Actor”, the widget would list “Actor” then “Tom Hanks”. Even though there are many other actors in the taxonomy of “Actors”, only the ones from the current page, are listed.

Related posts

Leave a Reply

2 comments

  1. This is a simple conversion of the Taxonomy terms list plugin to a widget:

    class WPSE_5394_Widget extends WP_Widget
    {
        public function __construct()
        {
            parent::__construct( 'wpse5394_widget', 'Taxonomy Terms List' );
        }
    
        public function widget( $sidebar_args, $widget_options )
        {
            if ( ! is_single() ) {
                // I don't think we can display anything sensible on a page with multiple posts
                return;
            }
    
            $output = $sidebar_args['before_widget'];
    
            // If we want to use this we should provide a way to set the title
            if ( ! empty( $widget_options['title'] ) ) {
                $output = $sidebar_args['before_title'] . $widget_options['title'] . $sidebar_args['after_title'];
            }
    
            // This is the meat of the function: get the taxonomies we want to display, and get the terms for each taxonomy
            $taxonomy_names = apply_filters( 'wpse5394_taxonomies', array_fill_keys( get_taxonomies(), true ) );
            $taxonomy_terms = '';
    
            foreach ( $taxonomy_names as $taxonomy_name => $dummy ) {
                $taxonomy = get_taxonomy( $taxonomy_name );
                $before = apply_filters( 'wpse5394_taxonomy_before', '<p>' . $taxonomy->label . ': ', $taxonomy );
                $sep = apply_filters( 'wpse5394_taxonomy_sep', ', ' );
                $after = apply_filters( 'wpse5394_taxonomy_after', '</p>' );
                $terms = get_the_term_list( 0, $taxonomy_name, $before, $sep, $after );
                if ( $terms ) {
                    $taxonomy_terms .= $terms;
                }
            }
    
            if ( ! $taxonomy_terms ) {
                // No taxonomy terms will be displayed - don't display the widget
                return;
            }
            $output .= $taxonomy_terms;
    
            $output .= $sidebar_args['after_widget'];
    
            echo $output;
        }
    }
    
    add_action( 'widgets_init', 'wpse5394_widgets_init' );
    function wpse5394_widgets_init()
    {
        register_widget( 'WPSE_5394_Widget' );
    }
    

    Most options can be set via filters, like the taxonomies you want to display:

    add_filter( 'wpse5394_taxonomies', 'wpse5394_taxonomies' );
    function wpse5394_taxonomies( $taxonomies )
    {
        unset( $taxonomies['category'] );
        return $taxonomies;
    }
    
  2. if i am not wrong you want something like this:
    there is a plugin which displays the custom taxonomy which is there selected for the corresponding post
    but it will display below the post not in the widget area

    you can have a look at it
    Taxonomy terms List

    this will display all the terms of taxonomies which you have selected