How to set tag name as Page Header

Gopala Padibandla asked on September 5, 2016 09:04

Hi there, I am following this documentation to set up Tag Cloud webpart https://docs.kentico.com/display/K9/Displaying+tags+on+a+page

I have a tag cloud on product listing pages on my site that goes to a tag results page which displays products that contain that chosen tag. I want to put a header at the top of that results page that says something like "Products Tagged As: (insert tag name here)"

The page URL contains the tagID variable, if that helps: Popular-Tags.aspx?tagid=142

I am using Portal Engine template. I tried following snippet

{% GlobalObjects.Tags.Where("TagID = " + ToInt(QueryString.GetValue("tagid", 0))) %}

It doesnt seem to be working. Any help is much appreciated.

Kind Regards, Gopala

Correct Answer

Jan Hermann answered on September 5, 2016 10:42

Please use this macro instead:

{%foreach(g IN SiteObjects.TagGroups){foreach(t IN g.Tags){if(t.TagID==ToInt(QueryString.tagid)){return t.TagName;}}}|(identity)GlobalAdministrator%}

1 votesVote for this answer Unmark Correct answer

Recent Answers


Richard Sustek answered on September 5, 2016 09:36 (last edited on December 10, 2019 02:30)

Tags are not accessible via GlobalObjects. Actually Tags are not accessible by macros at all. The only way I was able to make this work is via a Custom macro method that receives TagID as a parameter and then returns the tag name. To create a custom macro method see this article.

Try using a macro method like:

    [MacroMethod(typeof(string), "Gets Tag name based on given TagID", 1)]
    [MacroMethodParam(1, "TagID", typeof(int), "TagID")]
    public static object GetTag(EvaluationContext context, params object[] parameters)
    {
        switch (parameters.Length)
        {
            case 1:
                // get tag id
                int tagID = ValidationHelper.GetInteger(parameters[0], 0);

                if (tagID == 0)
                {
                    return null;
                }

                var tag = TagInfoProvider.GetTagInfo(tagID);

                if (tag == null)
                {
                    return null;
                }

                return tag.TagName;

            default:
                // No other overloads are supported
                throw new NotSupportedException();
        }
    }

And then call it like:

{% String.GetTag(ToInt(QueryString.tagid)) |(identity)GlobalAdministrator%}

However be aware that this macro goes to DB each time it is called (= cache it) and it is using default "string" namespace which is not best practice. Ideally you should create your own namespaces for custom macro methods.

1 votesVote for this answer Mark as a Correct answer

Gopala Padibandla answered on September 5, 2016 22:54

Thank you so much Richard, really appreciate your help. But Jan's macro does the work what I need. Once again thanks guys much appreciated.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.