Add Author Profiles to your Articles Blog

Add Author Bio pages to your MODX Articles Blog

If you have a MODX Articles blog, the default action if you click on the author of an article is to show you other articles by that author. What if you'd rather have it link to an author bio page (or have both a link to more of the author's articles *and* an author bio page link)?

Here's a relatively easy way for you to provide an author bio link for each article. The trick is to either create a bio page for each author named with the author's ID, or create a single page to show the bio for any user with a snippet and send the author's ID to that snippet.

Finding the Code

Look in your Article template Tpl chunk for some code that looks something like this (search for "Posted on"):

<p class="post-info">
<span class="left">Posted on [[*publishedon:strtotime:date=`%b %d, %Y`]] by <a href="[[~[[*parent]]]]author/[[*publishedby:userinfo=`username`]]">[[*publishedby:userinfo=`username`]]</a></span>
[[*articlestags:notempty=`<span class="tags left">&nbsp;| Tags: [[+article_tags]]</span>`]]
[[+comments_enabled:is=`1`:then=`&nbsp;| <a href="[[~[[*id]]]]#comments" class="comments">Comments ([[+comments_count]])</a>`]]

This is the section of that code that shows the author's other articles:

by <a href="[[~[[*parent]]]]author/[[*publishedby:userinfo=`username`]]"> . . . </a>

Your Tpl chunk may not look exactly like this one, but it's the part inside the double quotes you want to change to show the author's bio page instead. When MODX sees the original tag, it converts it to a link to the main blog page, followed by the authors username. When Articles sees that, it knows to show the author's other articles. Notice that there's no / before the 'author' reference. Typically, the main blog page is a container and the container suffix is set to / so it will automatically have one at the end of the parent URL.

Changing the Code

You can make that href point anywhere you want. How you do it depends on whether you want to create a separate new resource with a profile for each author, or whether you want to use the Profile snippet (part of the Login extra) to show the author information, in which case you only need a single page.

Using a Separate Page for Each Bio

If you have separate pages, you can create a container document (not an Article) that will be a parent to the profile pages (let's say its ID is 12), and put the profile pages below it in the tree. Each one's pagetitle will be 'Author##', where ## is the author's ID. Make sure the parent is marked as a container.

Then change the href to:

<a href="[[~12]]Author[[*publishedby]]"> . . .</a>

The publishedby field holds the ID of the user who published the resource. If that's not the author, you can use the createdby field.

Using a Single Page for All Bios

If you want to use the Profile snippet (a much more efficient method unless you want to hand-create each author's bio page), you'd send everyone to the same page (again, let's say its ID is 12), then use the Profile snippet to set placeholders for the particular user you want to show.

For security, I would put a tag for a custom snippet (called SaveAuthorId) in that Article Template that writes the author's ID to a $_SESSION variable rather than exposing it as a parameter in the URL.

/* SaveAuthorId */
$_SESSION['AuthorId'] = $modx->resource->get('publishedby');
return '';

In that case, the href would look like this:

<a href="[[~12]]"> . . .</a>

For the landing page (12), you can create a complimentary snippet to get the author ID from the $_SESSION variable:

return $_SESSION['AuthorId'];

Then, you can use that in the Profile snippet tag like this:

[[!Profile? &user=`[[!GetAuthorId]]`]]

You'll have to put placeholders on the page for the various user fields you want to show. The Profile snippet will set placeholders for the username and all fields in the user Profile. It will set placeholders for all extended fields as well if you add the &useExtended=`1` property in the Profile snippet tag.

Having Your Cake and Eating it Too

What if you want both a link to the author's other pages *and* a link to the author's bio page? There's no reason you can't have both. Just leave the original link alone and add another link somewhere else in the Tpl chunk (maybe right after the other one) that links to the bio page as described above.

One Last Security Note

If you're using a separate resource for each user's bio page, remember that the pagetitle contains the user ID, which you might not want to expose. I would not show the pagetitle in the template for the user bio page and I'd also use a custom alias for the page that didn't include the user's ID.

See the Profile snippet documentation for more information.


Comments (0)

Please login to comment.