Displaying blog category in list template

Posted by Community Admin on 03-Aug-2018 20:35

Displaying blog category in list template

All Replies

Posted by Community Admin on 26-Mar-2013 00:00

I want to display the blog category on my list page. I have to following control on the list template but some blogs are associated with multiple categories. I only want to display the first category. Is this possible?

Also, can I make them display as hyperlinks?

<sitefinity:HierarchicalTaxonField ID="HierarchicalFieldControl" runat="server" TaxonomyId="E5CD6D69-1543-427b-AD62-688A99F5E7D4" DisplayMode="Read" WebServiceUrl="~/Sitefinity/Services/Taxonomies/HierarchicalTaxon.svc" Expanded="false" TaxonomyMetafieldName="Category" ExpandText="ClickToAddCategories" BindOnServer="true" />

Posted by Community Admin on 28-Mar-2013 00:00

Hi Jeff,

You could try to do the following:

  • Create a new template for your List view (blog posts).
  • In the ItemDataBound event you get the value inside the Categories of the BlogPost
  • For each Category you will generate a navigate url
  • You concat all Categories, or just one in your case, and render them as one LiteralControl

Create a template
So the first step is to create a template. Inside this template you just add a Literal control that will hold the Category links:

<asp:Literal ID="categories" runat="server"></asp:Literal>

Get the categories in the ItemDataBound event
If there is no ItemDataBound event, you just add a new event on Page_Load or in the .ascx markup.

protected void Repeater_ItemDataBound(object sender, Telerik.Web.UI.RadListViewItemEventArgs e)
    
   var data = ((RadListViewDataItem)e.Item).DataItem as BlogPost;
   var placeholder = e.Item.FindControl("categories") as Literal;
   placeholder.Text = RenderFlatTaxaAsLink(data, "Tags");

You see I use a method here called RenderFlatTaxaAsLink, which is used for Tags, but you can change it to use it for Categories (Hierarchical).

In this method I do the following:

  1. First I define the BaseUrl to use when concatenating my navigate urls
  2. I get the Guids from my TaxonomyFieldName, in my case 'Tags', in your case Categories
  3. Then I lookup the Taxon for each of the Guids and concat an ordinary anchor tag
  4. I place them all in one variable, which is then bind to the Literal Control

public string RenderFlatTaxaAsLink(BlogPost post, string taxonomyFieldName)
 
   var baseUrl = string.Empty;
   var currentProvider = SiteMapBase.GetCurrentProvider();
 
   if (currentProvider == null || currentProvider != null && currentProvider.CurrentNode == null) return string.Empty; else
    
      var currentNode = currentProvider.CurrentNode as PageSiteNode;
      if (currentNode == null) baseUrl = currentProvider.CurrentNode.Url; else
         var firstPageDataNode = RouteHelper.GetFirstPageDataNode(currentNode, true);
         if (!currentNode.IsGroupPage || !(firstPageDataNode.Url != currentProvider.CurrentNode.Url)) baseUrl = currentProvider.CurrentNode.Url; else
            baseUrl = firstPageDataNode.Url;
         
      
   
 
   var tagList = "";
   var tags = post.GetValue<TrackedList<Guid>>(taxonomyFieldName);
   if (tags != null && tags.Count() > 0)
      tagList = " | ";
      var taxManager = TaxonomyManager.GetManager();
      foreach (var tag in tags)
         var t = taxManager.GetTaxon<FlatTaxon>(tag);
         var url = string.Format("0/-in-1/2/3", VirtualPathUtility.ToAbsolute(baseUrl), t.Taxonomy.Name, t.Taxonomy.Name, t.UrlName);
         var link = string.Format("<a class='tag' href='0'><span>1</span></a>", url, t.Title).ToString();
         tagList += link;
      
   
 
   return tagList;
 

Inside this method you can of course limit the results to just one taxonomy.

Hope this helps!

Kind regards,
Daniel

Posted by Community Admin on 29-Mar-2013 00:00

Hello,

Thank you Daniel for sharing your solution with community. I hope that Jeff and other clients will find it useful.

Kind regards,
Stefani Tacheva
the Telerik team

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 29-Mar-2013 00:00

No problem!

For anyone who likes to download the whole solution, find the link to my GitHub account on the bottom of my blogpost about this topic: http://knstr.it/XgraUX

Kind regards,
Daniel

Posted by Community Admin on 03-Apr-2013 00:00

Hi Daniel,

Thank you again. Really useful blog post. It is good to share your knowledge, articles and solutions in the forum. Your help is much appreciate.

Greetings,
Stefani Tacheva
the Telerik team

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 18-Apr-2013 00:00

This worked well. Thanks Daniel.

Posted by Community Admin on 15-Sep-2014 00:00

Hi

I can't get this to work with my blog list. I have this code in my template, where do I make the changes described above.

01.<%@ Control Language="C#" %>
02.<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.ContentUI" Assembly="Telerik.Sitefinity" %>
03.<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.Comments" Assembly="Telerik.Sitefinity" %>
04.<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI" Assembly="Telerik.Sitefinity" %>
05.<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.PublicControls.BrowseAndEdit" Assembly="Telerik.Sitefinity" %>
06.<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
07.<%@ Import Namespace="Telerik.Sitefinity" %>   
08. 
09.<telerik:RadListView ID="Repeater" ItemPlaceholderID="ItemsContainer" runat="server" EnableEmbeddedSkins="false" EnableEmbeddedBaseStylesheet="false">
10.    <LayoutTemplate>
11.        <sf:ContentBrowseAndEditToolbar ID="MainBrowseAndEditToolbar" runat="server" Mode="Add"></sf:ContentBrowseAndEditToolbar>
12.        <ul class="sfpostsList sfpostListTitleDateSummary">
13.            <asp:PlaceHolder ID="ItemsContainer" runat="server" />
14.        </ul>
15.    </LayoutTemplate>
16.    <ItemTemplate>
17.        <li class="sfpostListItem">
18.             
19.            <h2 class="sfpostTitle">
20.                <sf:DetailsViewHyperLink TextDataField="Title" ToolTipDataField="Description" runat="server" />
21.            </h2>
22.             
23.            <div class="sfpostAuthorAndDate">
24.                <asp:Literal ID="Literal2" Text="<%$ Resources:Labels, By %>" runat="server" />
25.                <sf:PersonProfileView runat="server" />
26.                <sf:FieldListView ID="PostDate" runat="server" Format=" | PublicationDate.ToLocal():MMM dd, yyyy" />
27.                <asp:Literal ID="categories" runat="server"></asp:Literal>
28.            </div>
29. 
30.            <sf:FieldListView ID="PostContent" runat="server" Text="0" Properties="Summary" WrapperTagName="div" WrapperTagCssClass="sfpostSummary" />
31. 
32.             
33.            <sf:ContentBrowseAndEditToolbar ID="BrowseAndEditToolbar" runat="server" Mode="Edit,Delete,Unpublish"></sf:ContentBrowseAndEditToolbar>
34.        </li>
35.    </ItemTemplate>
36.</telerik:RadListView>
37.<sf:Pager id="pager" runat="server"></sf:Pager>
38.<asp:PlaceHolder ID="socialOptionsContainer" runat="server" />
 

 

Thanks in advance.

 

Posted by Community Admin on 17-Sep-2014 00:00

Hello,

Further information for the sample could be found on Danie's blog post:

www.konstrui.nl/.../add-clickable-tags-for-each-blogpost-in-your-blogpost-list

You could find the custom template and solution in GitHub on the following URL.

If you have further questions regarding the sample you could contact the its publisher.

Regards,
Stefani Tacheva
Telerik

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

Posted by Community Admin on 23-Sep-2014 00:00

Hi Stefani

 I got it to work using the advice you gave me, but there is one small thing that I am still trying to do without success. What I need is to finally be able to create a menu with all the categories. If a user clicks on one of the categories then the blog filters on that category.

Also we have many blogs on our site, we want that menu to only include the categories for that blog.

Let me know if this is possible or what my options are.

Thanks in advance

Stewart

 

 

This thread is closed