Add a "more link"

Topics: ASP.NET 2.0, Business Logic Layer
Mar 8, 2012 at 2:31 PM

I've seen some blgengine.net blogs running and the posts on the fron page show a post with a "more" or "read more" link.  The front page shows an excerpt and the rest of the post is there when you click the link.  I've looked to see if there is any docs on this but I can't find anything.  How is this done.

 

Thanks in advance. 

Mar 8, 2012 at 4:59 PM

[more]

Mar 8, 2012 at 5:30 PM
Edited Mar 8, 2012 at 5:53 PM

If you are thinking about customizing the more link, you can see how it's coded if you have the BE source files, it's BlogEngine.Core/Web/Controls/PostViewBase.cs 

You can override this file in your theme folder, taking the Standard theme as an example.

In the Standard theme folder you will find a PostView.ascx file. The first line in this file will read: 

<%@ Control Language="C#" AutoEventWireup="true" EnableViewState="false" Inherits="BlogEngine.Core.Web.Controls.PostViewBase" %> 

Change this to reference your own code behind file e.g. 

<%@ Control Language="C#" AutoEventWireup="true" EnableViewState="false" CodeFile="PostView.ascx.cs" Inherits="PostView" %> 

You will then need to create the code behind file "PostView.ascx.cs" and code it e.g.  

using System;

using System.Globalization;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using BlogEngine.Core.Web.Controls;

using BlogEngine.Core;

 

public partial class PostView : BlogEngine.Core.Web.Controls.PostViewBase

{
    public string Body
    {
        get
        {
            var post = this.Post;

            var body = post.Content; 

            if (this.ShowExcerpt)
            {

		//The more link, do what you want with this                
		var link = " <a href=\"" + post.RelativeLink + "\" ><span class='hidden'> "

                    + post.Title + ",</span>" + " " + Resources.labels.more + "\u00A0\u00BB</a>";
 

                if (!string.IsNullOrEmpty(post.Description))
                {

                    if (post.Description.Length > this.DescriptionCharacters && this.DescriptionCharacters > 0)
                    {
                        body = post.Description.Substring(0, this.DescriptionCharacters) + "..." + link;
                    }
                    else
                    {
                        body = post.Description.Trim() + link;
                    }
                }
                else
                {
                    body = Utils.StripHtml(body);

                    if (body.Length > this.DescriptionCharacters && this.DescriptionCharacters > 0)

                    {
                        body = string.Format("{0}...{1}", body.Substring(0, this.DescriptionCharacters), link);
                    }
                }
            } 

            var arg = new ServingEventArgs(body, this.Location, this.ContentBy);

            Post.OnServing(post, arg); 

            if (arg.Cancel)
            {
                if (arg.Location == ServingLocation.SinglePost)
                {
                    this.Response.Redirect("~/error404.aspx", true);
                }
                else
                {
                    this.Visible = false;
                }
            } 
            return arg.Body ?? string.Empty;
        }
    } 

    protected override void OnLoad(EventArgs e)
    { 
        Utils.InjectUserControls((PlaceHolder)this.FindControl("BodyContent"), this.Body);
    }
}

PS

Maybe you were thinking of something like this?

Expand post with more link

Mar 8, 2012 at 5:59 PM
Edited Mar 8, 2012 at 7:38 PM

Oh yes...thanks.  The link  you provided is kind of what I'm looking for.  His "Read More" link kind of expands the post but I want it to just route to the page where the article resides similar to when you click the title of othe article. 

 

Your code is also helpful but I see some of the the title of the article is in the link.  How do I change it to just "read more"? 

Mar 8, 2012 at 8:44 PM

Yes, the post title is part of the link in this case, but it gets taken off screen with the CSS class hidden (Better than generic more link, I think). However, it can safely be removed.

//The more link, do what you want with this                
		var link = " <a href=\"" + post.RelativeLink + "\" >Resources.labels.more + "\u00A0\u00BB</a>";

You should be getting the default more link without making any changes though.

Make sure you have selected "Only show description of posts" in the blog admin panel, it's under Basic Settings ->Appearance

Mar 8, 2012 at 8:51 PM

Thanks alot for your help.  I have this now:

var link = " <a href=\"" + post.RelativeLink + "\" ><span class='hidden'> "

                    + "Read" + "</span>" + " " + Resources.labels.more + "\u00A0\u00BB</a>"; 

 

It displays pretty much what I wanted.

 

Now if I could just get the facbook comments to work in the posts I'm complete with the development part.

Thanks again.