How to add related post to Blogengine feed

Topics: Controls
Aug 18, 2008 at 12:41 AM
Edited Aug 18, 2008 at 12:46 AM
1.     First we should add three method, GetTagsString(),GetRelatedPostGuid()and GetRelatedPost().

Code:

        private static string GetTagsString(Guid id)

        {

            StringBuilder sb = new StringBuilder();

            string temple = "<a href=\"{2}tag/{0}/\" title=\"{1}\" rel=\"tags\">{1}</a>|";

            Post post = Post.GetPost(id);

            foreach (string tag in post.Tags)

            {

                sb.Append(string.Format(temple, HttpUtility.UrlEncode(tag), tag, Utils.AbsoluteWebRoot));

            }

            return "<br />tags:"+sb.ToString();

        }

        //Get Related Post ,default 10 .

        private static List<Guid> GetRelatedPostGuid(Guid id)

        {

            int flag = 0;

            List < Guid > list=new List<Guid>();

            list.Add(id);

            Post post=Post.GetPost (id);

            foreach (string tag in post.Tags)

            {

                if (flag <= 10)

                {

                    foreach (Post tpost in Post.GetPostsByTag(tag))

                    {

                        if (!list.Contains(tpost.Id)&&tpost.IsVisible)

                        {

                            list.Add(tpost.Id);

                            flag = flag + 1;

                        }

                    }

                }

                else

                {

                    break;

                }

            }

            list.Remove(id);

            return list;

        }

        //Get Related post html

        private static string GetRelatedPost(Guid gg)

        {

            StringBuilder sb = new StringBuilder();

            string templ = "<a href=\"{0}\" title=\"{1}\" rel=\"tags\">{1}</a><br />";

            List<Guid> list = GetRelatedPostGuid(gg);

            foreach (Guid g in list)

            {

                Post p = Post.GetPost(g);

                sb.Append(string.Format(templ,p.AbsoluteLink,p.Title));

            }

            return "<br /><strong> Related Post:</strong><br />" + sb.ToString();

        }

2. Change 

 writer.WriteElementString("description", content);

To     

 writer.WriteElementString("description", content + GetTagsString(publishable.Id) + GetRelatedPost(publishable.Id));



Done, you can see a demo: http://blog.119797.com

Also you can visitor this URL to get three method http://blog.119797.com/code/200808/SyndicationGenerator/cs