WordPress.org

Support

Support » Plugins and Hacks » [Plugin: Taxonomy Widget] Hide if no taxonomies

[Plugin: Taxonomy Widget] Hide if no taxonomies

  • Hello Michael,

    I am starting to use your Taxonomy Widget (which is very nice) but if I have a taxonomy that i chose to list, but on a given page it doesn’t use that taxonomy, is there any way to NOT have that list appear rather than having “No categories”?

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Michael Fields

    @mfields

    Thanks. Not sure I completely understand the trouble you are having though… Do you have a link?

    Hello Michael,

    Yes. So as I am building and testing different plugins, widgets, etc. I have thrown this playground together:
    http://hailpern.net/wordpressTest/?p=53
    Along the right column, I have your Taxonomy widget using both Categories and another one using Post Tags. As you can see, there are no Post Tags, Yet, the grouping Pos Tags still exists and explicitly states “No categories”

    Plugin Author Michael Fields

    @mfields

    Awesome. Thanks for that! Currently, there is no way to do this. I’ll need to re-factor the plugin to take this into account. Good catch!

    Oh wait – your widget isn’t showing what terms are related to the current page, but overal, what terms exist in a taxonomy!

    Oh my.

    Do you know of a widget that will show what terms ARE on a current page?

    Plugin Author Michael Fields

    @mfields

    I use something like this in my site’s redesign feel free to use it. It’s not a widget though:

    Add to functions.php

    /*
     * Must be used inside loop.
     * @since 2010-12-05
     */
    function mfields_list_terms( $taxonomy = 'category', $labels = array() ) {
    	global $post;
    	$labels = array_merge( array(
    		'singular' => 'Category',
    		'plural'   => 'Categories',
    		'override' => ''
    		), $labels );
    	if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
    		$terms = get_the_terms( $post->ID, $taxonomy );
    		if ( ! is_wp_error( $terms ) && is_array( $terms ) ) {
    			$label = $labels['plural'];
    			if ( !empty( $labels['override'] ) ) {
    				$label = $labels['override'];
    			}
    			else if ( 1 === count( $terms ) ) {
    				$label = $labels['singular'];
    			}
    			print "\n" . '<h2>' . $label . '</h2>';
    			print "\n" . '<ul>';
    			foreach( (array) $terms as $term ) {
    				$href = get_term_link( $term, $taxonomy );
    				if ( ! is_wp_error( $href ) && isset( $term->name ) ) {
    					print "\n" . '<li><a href="' . esc_url( $href ) . '">' . $term->name . '</a></li>';
    				}
    			}
    			print "\n" . '</ul>';
    		}
    	}
    }

    Add to template file:

    mfields_list_terms( 'topics', array(
    	'singular' => 'Topic',
    	'plural' => 'Topics'
    	) );

    I am sorry – this is my first time using wordpress (my old website that i am updating was a drupal CMS).

    Can only “widgets” exist in the side bars?

    Plugin Author Michael Fields

    @mfields

    Kinda… it really depends on how your sidebar is coded. It’s relatively easy to add code before or after the call to dynamic_sidebar() but if you need to put code somewhere inside the function call, the code would need to be delivered inside a widget.

    Ah I see… I finished writing a nice plugin last night that added some new shortcode calls – which might be a way to go with this.

    Can I add shortcode to some type of widget? Basically, a blank widget to whom, short code can just be written?

    Plugin Author Michael Fields

    @mfields

    I don’t really like to use shortcodes in Widgets. I don’t think that WordPress supports this out-of-the-box. I know there are ways to get around this, but I do not personally use them. You may want to start a new thread about that topic.

    If you add the following code to the code I posted above, you will have a really simple widget that only displays on single post views:

    class My_Themes_Taxonomy_Term_Widget extends WP_Widget {
    	function My_Themes_Taxonomy_Term_Widget() {
    		$widget = array(
    			'class' => 'My_Themes_Taxonomy_Term_Widget',
    			'description' => ''
    			);
    		$this->WP_Widget( 'My_Themes_Taxonomy_Term_Widget', 'My Themes Taxonomy Term Widget', $widget );
    	}
    	function widget( $args, $instance ) {
    		if ( is_single() ) {
    			mfields_list_terms( $taxonomy = 'topics' );
    		}
    	}
    }
    
    function register_my_themes_taxonomy_term_widget() {
    	register_widget( 'My_Themes_Taxonomy_Term_Widget' );
    }
    
    add_action( 'widgets_init', 'register_my_themes_taxonomy_term_widget' );

    I’d like to add my vote to not show the list when there are no posts currently using that taxonomy.

    I have a number of them that I’ve created for my client’s future possible use, but until she adds something that uses them, just a title shows up, which can be confusing. I could choose not to show the title, but I think it’s kind of important to include that when there actually are things in the dropdown (it’s not always obvious).

    It’s a wonderful plugin and just what I needed. I use it in combination with Custom Post Type List Widget (which shows a list of custom post types).

    Thanks for your help,
    Cyd

    Plugin Author Michael Fields

    @mfields

    @cydward – Thanks for your input. I agree with you here and will definitely address this in the next release. I’m currently working on updating my Taxonomy Images plugin and will be integrating some of it’s functionality into the taxonomy widget plugin when I am finished.

    That’s great, Michael!

    I’ve tried to get it not to create a listener if the taxonomy doesn’t have any posts (since getElementById throws a javascript error for the taxonomy that is null), but was unable to do it. I can’t even figure out where the global field is being set. sheesh)

    For now, I’ll just have to not create a widget in the sidebar until I know there are posts in it. So I add it, and if I just get a title, then I remove it.

    I’m really looking forward to having it not even spit out a title in the sidebar unless the taxonomy is actually populated with posts. I’m thinking that could all happen in one step, sort of? So you would still be able to create the widget in the sidebar, but if a taxonomy had no posts, it wouldn’t create output or a listener. And then later, when posts got added to that one, it would print the title and the list, and create the listener.

    Am I asking too much? I certainly haven’t been able to do it, but I’m really new at both php and javascript.

    I appreciate all you’re doing!
    Cyd

    Plugin Author Michael Fields

    @mfields

    @cyd Everything you’ve mentioned is completely on point and exactly how the plugin should function IMO. I don’t know how this eluded me when I created it. You will definitely see these changes in the next version which should be done pretty soon with 3.1 almost ready for release.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘[Plugin: Taxonomy Widget] Hide if no taxonomies’ is closed to new replies.