How to make an intelligent ‘read more’ link in PivotX
July 31st, 2010
If you read a blog, a lot of times you’ll see the introduction of a blog entry on the front page of the blog, followed by a ‘Read more’ link. That link will take you to the full text on the entry.
In a PivotX template, you can place that ‘Read more’ link in a template with the [[ more ]] template tag. You can even control what the link looks like with the parameter ‘text’, like so: [[more text="Continue reading..." ]]. There is a problem with this template tag though…
Let me explain what the issue is before we look at solving it:
The automagically generated link will contain ‘#bodyanchor’ at the end of the link url, so you can actually let the reader jump to the body text. I don’t like that, because in a lot of layouts it’s confusing, and the user ends up scrolling up, to then realize he’s already read that part, and he was redirected to the actual body. Also, there is no way of giving the link a separate class.
The solution is simple: just make a link to the blog entry (the ‘permalink’):
<a class="more-link" href="[[ link hrefonly=1 ]]">Read More »</a>
This will display something like ‘Read more »’, and now the link has a class called ‘more-link’ that you can use to style it.
However, sometimes you write blog posts that are not very long, and those entries might not have more text to display than just the introduction. Normally, the [[ more ]] tag would not output anything in that case. The above code will, and this can be quite dissapointing for visitors to your blog, and confusing too. After clicking the ‘read more’ link, it turns out there’s nothing more to read!
Thanks to the power of Smarty, there’s always a solution. Just program some intelligence in your template, and you can make the ‘read more’ link disappear when there is nothing after the introduction.
The solution is to use an [[ if ]] statement that looks if there’s more to read first:
[[ if $entry.body != "" ]]
<a class="more-link" href="[[ link hrefonly=1 ]]">Read More »</a>
[[ /if ]]
If you wrap your link like this, it will only show up if the body of the entry actually contains any text.
Note: I edited this post after publishing. I’ve been using the permalink for so long, I didn’t realize the [[ more ]] tag actually doesn’t display when there is no text in the body. Still I like to use the ‘permalink’ method for the ‘Read more’ link, and then you need the [[ if ]] statement…


That’s strange. Never had the need to do this. In PivotX installs I run, the ‘more link’ is only shown when there is actually more content.
Thanks for pointing that out Jarno, you’re right of course. I actually very rarely use the [[ more ]] tag, just because I like to give my anchor tags a class, and I don’t like the #bodyanchor part that is automatically added.
I rewrote part of the post.
Just two remarks:
1) You can use the “anchorname” parameter to set a different location for the read more link. Unfortunately, you can’t yet use that parameter to remove the hash tag/anchor completely (by setting it to an empty string).
2) You can use the “prefix” and “postfix” parameters to add a span (around the link) with a CSS class. I agree that there should be a class by default.
This is all documented at http://book.pivotx.net/page/app-b#anchor-more
PS! I’ll fix the two above short comings before the release of PivotX 2.1 – which is very close …
Thanks Hans! As you point out in your first remark, it is easy to change the location of the anchor tag, but the fact that you can’t completely remove it is indeed a short coming.
As for your second remark: The ‘prefix’ and ‘postfix’ parameters are there, but this results in another span or div or whatever you want around the link, while it would indeed be so much more logical to just add a class to the link.
In any case, the PS to your comment shows once more that the developers of PivotX are right on the money where it comes to making changes that are useful…