Is there a way to truncate a entry body into an excerpt with a {exp} tag?
I dont want to have another custom field just for an excerpt, I’d rather just take like the first 20 or 25 words and then put an ellipsis at the end for the preview.
This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.
The active forums are here.
May 06, 2011 2:36pm
Subscribe [6]#1 / May 06, 2011 2:36pm
Is there a way to truncate a entry body into an excerpt with a {exp} tag?
I dont want to have another custom field just for an excerpt, I’d rather just take like the first 20 or 25 words and then put an ellipsis at the end for the preview.
#2 / May 06, 2011 3:03pm
I don’t think that there is a built-in EE tag, but I’ve seen a number of extensions that handle this; see, for example:
#3 / May 06, 2011 3:45pm
I’d rather not use an addon, so I tried to add some PHP to do this (real quick test, its not finished). Only way I could figure out to get the value out was to use heredoc
<?php
//REQUIRES PHP TO BE ENABLED IN SETTINGS OF TEMPLATE
$news_body = <<<END_OF_DATA
{news_body}
END_OF_DATA;
echo substr($news_body, 0, 50);
?>#4 / May 07, 2011 1:14pm
Just out of interest I’m wondering why you don’t want to use an add-on?
There are some very capable ones out there and they help keep your templates nice and tidy and easy to read instead of having the PHP in the templates. Plugins are after all just PHP in exactly the same way as your code above would be but using a plugin allows you to keep your templates neat and tidy.
Just wondering what the reason is really though?
Best wishes,
Mark
#5 / May 09, 2011 1:18pm
Plugins often break or require updates over time, so I don’t like to rely on them too much. A lesson I’ve learned this from various CMSes and webapps I’ve used.
#6 / May 09, 2011 2:09pm
Plugins often break or require updates over time, so I don’t like to rely on them too much. A lesson I’ve learned this from various CMSes and webapps I’ve used.
Fair point although with these types of plugins they are ‘quite’ simple in the way they do things and do usually get updated for new version changes very quickly.
That said there’s not a lot that changes between point releases that affects this kind of plugin though so usually fairly safe in using these.
Best wishes,
Mark
#7 / May 09, 2011 10:34pm
There are also 1st party plugins developed by Elisalabs here: http://expressionengine.com/downloads/details/word_limiter/
#8 / May 09, 2011 11:37pm
I’ve been using Trunchtml for years without an issue. It’s a very stable add-on.
#9 / May 10, 2011 3:21pm
I’ve been using Trunchtml for years without an issue. It’s a very stable add-on.
i second TruncHTML, works great for me.
#10 / Apr 10, 2012 12:28pm
I’m not aware of any truncate plug-ins that are compatible with multiple site manager so I had to use php.