Technology, Smartphones, Games


BlogEngine tag based related posts

if you are using BlogEngine.NET for your blog and you are using the related posts control, you may need this.

Normally BlogEngine will decide the related posts from the body and heading, but it may not display related posts accurate on all posts, so the following change will display related posts based on the tags.

To change this you will have to edit the App_Code\Controls\RelatedPosts.cs

Replace the RenderControl function with the following function.

public override void RenderControl(HtmlTextWriter writer)
        {
            if (!BlogSettings.Instance.EnableRelatedPosts || this.Item == null)
            {
                return;
            }
 
            if (!RelatedPostsCache.ContainsKey(this.Item.Id))
            {
                // Grab the post tags and do a search
                Post post = (Post)this.Item;
                string tags = string.Empty;
                if (post.Tags.Count > 0)
                {
                    foreach (string tag in post.Tags)
                    {
                        tags += tag + " ";
                    }
                    tags = tags.Substring(0, tags.Length - 1);
                }
                var relatedPosts = Search.Hits(tags, false);
                // If no results, just do the search as normal by post title
                if (relatedPosts.Count < 1)
                {
                    relatedPosts = Search.FindRelatedItems(this.Item);
                }
                if (relatedPosts.Count <= 1)
                {
                    return;
                }
 
                this.CreateList(relatedPosts);
            }
 
            writer.Write(RelatedPostsCache[this.Item.Id].Replace("+++", this.Headline));
        }

Now this code is being used in this website for related post section. We have changed the style of the related post if you like the design change contact us we are ready to share that, or will post about it in near future anyway.

Read more here