Show all tags of an author in multiauthor wordpress website

Yesterday I was trying to create a function which would include all tags with number of post count in the author profile. For everyone who is using multi author blog and want to give the user ability to see the number of posts tagged with a certain keyword. You all are aware of uses of the tags and how it can help in creating more organized content. Tags are easier to manage from the end user perspective. I was trying to create a social network for students and teachers. Any user can easily post an article and tag it with certain keywords. For example, If a teacher wants to share his expertise on wordpress themes, he can write a post and tag it with "wordpres theme tutorial" whereas he can also write about the wordpress plugin and tag it with "wordpress plugin tutorial". Now I wanted to show top 5 recent posts by that author in his profile page and all tags that he used with the posts count.
Here is the result :
Note that It is recent 5 posts by a particular author and different tags he used. Let's see how you can call the function to show all tags by used by a particular author.

// Get Author's Tags
function get_author_post_tags($author_id,$taxonomy = 'post_tag'){
    //get author's posts
    $posts = get_posts(array(
        'author' => $author_id,
        'posts_per_page' => 5,
        '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) {

            $ts[$tag->term_id]['count'] = $ts[$tag->term_id]['count']  + 1;
                $ts[$tag->term_id] = array('count' => $ts[$tag->term_id]['count'], '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 class="mylabels"><h4>Tags</h4>';
    foreach ($ts as $term_id => $term_args) {
        echo '<li><a href="'.add_query_arg('tag',$term_args['slug'],$url).'">'.$term_args['name'].'<span class="bubble">'.$term_args['count'].'</span></a></li>';
    }
    echo '</ul>';
}


Copy the code in the functions.php file of your child theme. Let us understand the function a bit. It is a simple function which holds two parameter one is $author_id and other is tag. When you call this function you will have to pass the author id .
Like <?php $author_id= bp_displayed_user_id(); get_author_post_tags($author_id,$taxonomy = 'post_tag');?> . If you want to use the tags in the user profile of buddypress website, you can use the above php code.

I have not added any style you can use by selecting the class mylabel ul li and play arround it get it done. Class bubble control the look and feel of count number. Hope this will help you.

Comments

Popular posts from this blog

Wordpress on Bluemix - Create a wordpress website

Buddypress Demo Website - Learn Buddypress

IBM Bluemix Pricing For Wordpress Cloud Hosting