This project is read-only.

Generating links to previous/next posts

Topics: Controls
May 23, 2013 at 4:08 PM
I'd like to generate links at the bottom of every post that will take you forward or back through the posts, but I haven't found any examples. I know you can get the link to the current post using <%=Post.PermaLink %> or <%=Post.RelativeLink %> but I want something like Post.NextPost.Link and Post.PreviousPost.Link. We're using MS SQL, so I could just query it from the database, but I'm wondering if there's an easier way.
May 23, 2013 at 8:06 PM
This is taken directly from post aspx.cs in the blog root
/// <summary>
    /// Gets the next post filtered for invisible posts.
    /// </summary>
    private Post GetNextPost(Post post)
        if (post.Next == null)
            return null;

        if (post.Next.IsVisible)
            return post.Next;

        return GetNextPost(post.Next);

    /// <summary>
    /// Gets the prev post filtered for invisible posts.
    /// </summary>
    private Post GetPrevPost(Post post)
        if (post.Previous == null)
            return null;

        if (post.Previous.IsVisible)
            return post.Previous;

        return GetPrevPost(post.Previous);
So you were pretty close, just Next instead of NextPost i.e. post.Next.RelativeLink
May 23, 2013 at 8:11 PM

So it was right in front of my face the entire time. Is there a place where these methods are documented? The only thing I found was this, but there's only a handful listed there. But seriously, I would have given up and written my own before I ever found that.
May 23, 2013 at 8:24 PM
Edited May 23, 2013 at 8:45 PM
Not that I'm aware of, BlogEngine is pretty damned good, but the documentation is somewhat sparse.
I've actually done that a couple of times, written my own and then found a pre-existing method.

Probably should have mentioned, there's a checkbox setting in Admin to show or hide post navigation links, which appear at the top of the post by default. Depending on what you intended to do with the next/previous links (show next/previous post images with links for example) you might just get away with moving the placeholder "phPostNavigation" in post.aspx.