WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display a list of Tags (3 posts)

  1. sdjentertainment
    Member
    Posted 4 years ago #

    Hello Fellow WordPressers!

    Just wondering if anyone could help me out or know of a tutorial which they can point out to me. I was wondering if there is anyway to display a list of the most common tags for a specific category.

    Basically I want a list with the 15 most popular tags within a specific category.

    Any Help would be greatly appreciated.

    Cheers,
    Simon

  2. MichaelH
    Member
    Posted 4 years ago #

    Stole some code from the tdo-tag-fixes plugin:

    <?php
    //get all posts in a given category
    //then store a count of posts by tag in array
    //sort array descending by number of posts
    //display just the number of tags needed
    $tag_counts = array();
    $max_tags = 15;
    $cat_id = get_cat_ID('Uncategorized1');
    $args=array(
      'cat' => $cat_id,
      'post_type' => 'post',
      'post_status' => 'publish',
      'nopaging' => true,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if($my_query->have_posts()) {
      $posts = $my_query->get_posts();
      foreach($posts as $post) {
        $post_tags = wp_get_post_tags($post->ID);
        if(!empty($post_tags)) {
          foreach($post_tags as $post_tag) {
            $tag_counts[$post_tag->term_id]+= 1;
          }
        }
      }
    }
    wp_reset_query();  
    
    if ($tag_counts) {
      arsort($tag_counts); //sort array in reverse order by number of posts
      foreach($tag_counts as $key => $tag_count) {
        $count++;
        if ( $count <= $max_tags) {
          $term = get_term_by('id',$key, 'post_tag');
          echo '<p>' . '<a href="' . get_term_link( $term, 'post_tag' ) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> has ' . $tag_count . ' post(s). </p> ';
        }
      }
    }
    ?>
  3. sdjentertainment
    Member
    Posted 4 years ago #

    BRILLIANT!

    Cheers,
    Simon

Topic Closed

This topic has been closed to new replies.

About this Topic