How to show the number of items in the Tag Cloud Web Part

   —   
This article describes how to show the number of items in brackets after particular tag words in the Tag cloud web part, instead of the default font-size distinction.
As you already know, the Tag cloud web part is used to display tags that are associated with the current document. Tags are displayed in the form of links and those with higher occurrence are displayed in higher font size. If the site visitor clicks on some of the links, a list of all documents tagged with the given tag will be displayed.

Its standard appearance is as follows:




You may want to have all the tags of the same size, but with info about their count instead (or keep them in a different size, but add their count as well). Something like this:




Two steps must be taken in order to achieve this appearance. First, you need to navigate to the web part properties dialog and set “Minimal tag font size” and “Maximal tag font size” to the same values. Then, all tags will be of the same size, regardless of how often they are used with your documents.

Second, you have to modify the code-behind file of the web part. Usually it is placed in <project folder>\CMSWebParts\TaggingCategories\TagCloud.ascx.cs.

Here you can find a variable named “tagCount,” which represents the number of occurrence of the given word in the DataSet which contains all tags according to the web part settings.

You may notice that this web part has no transformation for displaying content. Its output is rendered via the following lines of code:

// Creates a link with a query string parameter
string paramUrl = UrlHelper.AddParameterToUrl(url, QueryStringName, ValidationHelper.GetString(dr["TagID"], ""));

sb.Append("&lt;span&gt;&lt;a href=\"" + HTMLHelper.HTMLEncode(paramUrl) + "\" style=\"font-size:" + pixelSize.ToString() + "px;\" &gt;" + HTMLHelper.HTMLEncode(dr["TagName"].ToString()) + "&lt;/a&gt; &lt;/span&gt;\n");

You can study the parameter of the Append method to see how the tag-link itself is composed. All we need to do is add the following line of code in the Append method, which will ensure that the proper item count will be displayed:

String.Format("({0})", tagCount)

So, the whole method should be like this:

sb.Append("&lt;span&gt;&lt;a href=\"" + HTMLHelper.HTMLEncode(paramUrl) + "\" style=\"font-size:" + pixelSize.ToString() + "px;\" &gt;" + HTMLHelper.HTMLEncode(dr["TagName"].ToString()) + " " + String.Format("({0})", tagCount) + "&lt;/a&gt; &lt;/span&gt;\n");
-rm-


See also:

Applies to: Kentico CMS 5.x
Share this article on   LinkedIn