1

I would like to list tags with count in author profile page just like this page

If the user click the tag, then I would like to list the tagged posts.

Can someone help me?

1 Answer 1

2

Here you go:

function get_author_post_tags_wpa78489($author_id,$taxonomy = 'post_tag'){
    //get author's posts
    $posts = get_posts(array(
        'author' => $author_id,
        'posts_per_page' => -1,
        'fields' => 'ids'
        )
    );

    $ts = array();

    //loop over the post and count the tags
    foreach ((array)$posts as $p_id) {
        $tags = wp_get_post_terms( $p_id, $taxonomy);
        foreach ((array)$tags as $tag) {
            if (isset($tags[$tag->term_id])){ //if its already set just increment the count
                $ts[$tag->term_id]['count'] = $ts[$tag->term_id]['count']  + 1;
            }else{ //set the term name start the count
                $ts[$tag->term_id] = array('count' => 1, 'name' => $tag->name, 'slug' => $tag->slug);
            }
        }
    }

    //so now $ts holds a list of arrays which each hold the name and the count of posts 
    //that author have in that term/tag, so we just need to display it
    $url = get_author_posts_url($author_id);
    echo '<ul>';
    foreach ($ts as $term_id => $term_args) {
        echo '<li><span class="count">'.$term_args['count'].'</span> <a href="'.add_query_arg('tag',$term_args['slug'],$url).'">'.$term_args['name'].'</a></li>';
    }
    echo '</ul>';
}
7
  • Can you tell me how to list the tagged post url when the user click the tag link?. As of now it doesn't show post url Commented Jan 6, 2013 at 23:55
  • Also please fix the closing ul tag. Commented Jan 6, 2013 at 23:57
  • Fixed, are you getting the posts? its working on my end, see (.add_query_arg('tag',$term_args['slug'],$url))
    – Bainternet
    Commented Jan 7, 2013 at 7:03
  • No, I'm not getting the posts. When I click the tag it looks like this http://example.com/author/authorusername?tag=tagname. But I'm not seeing any posts. Is that function should be placed within the loop? Commented Jan 8, 2013 at 10:00
  • place it anywhere you want!, what you permalink?
    – Bainternet
    Commented Jan 8, 2013 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.