WordPress.org

Ready to get started?Download WordPress

Forums

Taxonomy Terms List
Displaying Taxonomy Terms with Count > 1 (4 posts)

  1. iftomkins
    Member
    Posted 3 years ago #

    Hi Michael. I was wondering if you knew of a way I could only display taxonomy terms which have counts greater than 1 (or hide all taxonomy terms with counts equal to 1 or fewer). This would be to avoid the problem of arriving at the end of the post, clicking on a taxonomy term that had been assigned to that post, and then ending up right back at the same post you just read.

    What do you think?

    http://wordpress.org/extend/plugins/taxonomy-terms-list/

  2. MichaelH
    Member
    Posted 3 years ago #

    Can't say how the plugin does it...didn't test this, but might try this in your loop:

    <?php
    $taxonomy = 'post_tag';
    $args='';
      $terms = wp_get_post_terms($post->ID , $taxonomy, $args);
      if ($terms) {
        foreach ( $terms as $term ) {
          if ($term->count > 1) {
            echo '<p><a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> has ' . $term->count . ' post(s). </p> ';
          }
        }
      }
    ?>
  3. iftomkins
    Member
    Posted 3 years ago #

    Thanks, @MichaelH.

    I tried putting it in my loop, but it affected neither the plugin's list of related taxonomy terms, nor my customized wp_list_categories function in the sidebar that displays my custom taxonomy terms.But since I wasn't really sure exactly how to "plug in" the code, so to speak, I might be getting it wrong. What does the code you proposed connect to? What functions or lists of terms will be restricted by it?

    Ideally I was looking for a way to add that type of condition to Michael Field's Taxonomy Term List plugin. Maybe one day! :)

  4. iftomkins
    Member
    Posted 3 years ago #

    MichaelH - here is the plugin code, in its entirety. Any thoughts on adapting your suggested chunk of code to restrict what taxonomy terms are listed by the plugin code below? Thanks!

    if( !function_exists( 'pr' ) ) {
    	function pr( $var ) {
    		print '<pre>' . print_r( $var, true ) . '</pre>';
    	}
    }
    
    if( !function_exists( 'mfields_taxonomy_terms_list' ) ) {
    	add_filter( 'the_content', 'mfields_taxonomy_terms_list' );
    	function mfields_taxonomy_terms_list( $c ) {
    		global $post;
    		$o = '';
    		$terms = array();
    		$lists = array();
    		$custom_taxonomy_names = array();
    		$custom_taxonomies = mfields_get_custom_taxonomies();
    		if( !empty( $custom_taxonomies ) )
    			foreach( $custom_taxonomies as $name => $config )
    				$custom_taxonomy_names[] = $config->name;
    		if( !empty( $custom_taxonomy_names ) )
    			$terms = get_terms( $custom_taxonomy_names );
    		foreach( $custom_taxonomies as $name => $config )
    			$o.= get_the_term_list( $post->ID, $name, $before = '<div class="taxonomy-term-list"><div class="mfields-taxonomy-term-list-name">Related ' . $config->label . ':</div> ', $sep = '<br />', $after = '</div>' );
    		if( is_single() || is_page() )
    			return $c . $o;
    		return $c;
    	}
    }
    
    $taxonomy = 'post_tag';
    $args='';
      $terms = wp_get_post_terms($post->ID , $taxonomy, $args);
      if ($terms) {
        foreach ( $terms as $term ) {
          if ($term->count > 1) {
            echo '<p><a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> has ' . $term->count . ' post(s). </p> ';
          }
        }
      }
    
    if( !function_exists( 'mfields_get_custom_taxonomies' ) ) {
    	function mfields_get_custom_taxonomies( ) {
    		global $wp_taxonomies;
    		$custom_taxonomies = array();
    		$default_taxonomies = array( 'post_tag', 'category', 'link_category' );
    		foreach( $wp_taxonomies as $slug => $config )
    			if( !in_array( $slug, $default_taxonomies ) )
    				$custom_taxonomies[$slug] = $config;
    		return $custom_taxonomies;
    	}
    }
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic