Posts by Author in Custom Page

Oct 27, 2010 at 4:14 PM

Hi there:

I have created a few pages that each have a write up on a different author. Below the write up, the client would like to show the blog posts written by that author. Is there something I can plug into a custom page in order to do this?

Thanks.

 

Coordinator
Oct 27, 2010 at 6:54 PM
Edited Oct 27, 2010 at 9:37 PM

If by "custom page" you mean the a BE Page (or even your own ASPX page), you can use a Control to output the author posts.  Create a new file in the blog root folder called "author_posts.ascx".  Put the code below into it.  If you're using a BE Page, you can inject this Control into the Page by adding the following into the Page WYSIWYG editor:

[usercontrol: ~/author_posts.ascx AuthorName=admin]

The last part of that sets the AuthorName property to "admin".  You would want to replace admin with the name of the author.  If you created your own ASPX page, you can still reference this control, the normal way you would insert a control into an ASPX page.

<%@ Control Language="C#" ClassName="author_posts" %>
<%@ Import Namespace="BlogEngine.Core" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>

<script runat="server">

    private string _authorName;
    public string AuthorName
    {
        get { return _authorName; }
        set { _authorName = value; }
    }
    
</script>

<% if (!string.IsNullOrEmpty(this.AuthorName))
   {
       List<Post> authorPosts = Post.GetPostsByAuthor(this.AuthorName);
       if (authorPosts != null && authorPosts.Count > 0)
       {
           Regex imgRgx = new Regex("<img[^>]+>", RegexOptions.IgnoreCase);
           %>
           Posts by this Author
           <ul>
           <%
           foreach (Post post in authorPosts)
           {
               string postContent = imgRgx.Replace(post.Content, " ");
               %>
                    <li>
                        <a href="<%= post.RelativeLink %>"><%= post.Title %></a>
                        <div>
                            by <%= post.Author %> on <%= post.DateCreated.ToString("g") %>
                        </div>
                        <% if (post.Categories.Count > 0) { %>
                            <div>
                                filed under:
                                <%
                                    for (int i = 0; i < post.Categories.Count; i++)
                                    {
                                %>
                                    <%= i > 0 ? ", " : string.Empty %>
                                    <%= post.Categories[i].Title %>
                                <%
                                    }
                                %>
                            </div>
                        <% } %>
                        <div>
                            <% if (postContent.Length > 250)
                               { %>
                                <%= postContent.Substring(0, 250) + "..."%>
                            <% } else { %>
                                <%= postContent%>
                            <% } %>
                        </div>
                    </li>
               <%
           }
           %>
           </ul>
           <%
       }
    }
%>

Oct 27, 2010 at 7:26 PM

Hi Ben:

Thanks for all your help. I am not too familiar with this sort of coding yet. This works well. Is there a way to add a little more info to each post besides the title for this? Perhaps the Published by, the Categories its under and the date, and then maybe the first paragraph. This should be the last request, as the rest is completed. Thanks again.

Coordinator
Oct 27, 2010 at 8:11 PM

I updated my last post so it outputs more details like Published by, Categories, date, and an excerpt.

Oct 27, 2010 at 8:35 PM

Thanks again Ben. One final question. Is there a way to make it so the article image doesn't appear if its in the post? Probably not, but I thought I'd ask.

Coordinator
Oct 27, 2010 at 9:38 PM

That's possible.  I updated the code again (including a new line at the top, so best to use the whole thing).  For the excerpts it outputs, it will remove any images that may be there.

Oct 28, 2010 at 2:19 AM
Edited Oct 28, 2010 at 2:20 AM

Hi Ben:

In the theme I use, it comes with a box on the right hand side of the post that shows the date for each posting. I was trying to add it to the existing code in the author_post.aspx. Here is the code from the postview.aspx that I would like to add in if possible, however I am just getting error messages.

 

<p class="date">
        <span class="month"><%=Post.DateCreated.ToString("MMM")%></span><span class="day"><%=Post.DateCreated.ToString("dd")%></span><span class="year"><%=Post.DateCreated.ToString("yyyy")%></span>
        </p>
Coordinator
Oct 28, 2010 at 4:19 AM

If you're putting that code within the "foreach" loop (which I'm guessing you are), then the main thing that's needed is to change the casing for "Post" so it becomes "post" (it looks like that's needed about 3 times in that snipped you posted).

Otherwise, the only other important part is just making sure that code is outside the <% ... %> code blocks.  Kind of hard to see with all that jumbled up markup and ASP.NET code!  If you put it right after one of the closing </div> tags, that should be safe.

If you're still getting errors, just write back and I can insert that code you have into my original example.

Oct 28, 2010 at 4:46 PM

That works, thanks Ben.