Trimming Any Content to a Number of Words in Movable Type

| No Comments | No TrackBacks |
As you may or may not know, the <mt:entrybody> tag (and some others) has the very usefull 'words' attribute that allows you to automatically trim its output to a specified number of words.  Want to show just 20 words?  Just put in <mt:entrybody words="20"> and you are done.  Unfortunately, this 'words' attribute is not one of the 'global modifiers' that work on any tag.  Tough luck?  Not at all...
Again, the Swiss Army knife of template tag modifiers comes to the rescue: regex_replace.

Suppose you have set up a 'Bio' custom field for your Authors, so you can display more information about who they are.  But on your main page you don't want to display all of this text, as it would quickly fill up the page.  Here is what you can do to show just the first fifteen words:

<mt:authordatabio regex_replace="/^((.+?\s+){15}).*/gsi","\1">​

Note that the 'regex_replace' part will work on any tag, and you can vary the number of words by changing the number from '15' to anything you want.

No TrackBacks

TrackBack URL: https://www.movabletips.com/cgi-bin/mt/mt-tb.cgi/28

Leave a comment


Movable Type Help?

"Can my Movable Type issue or project be taken care of by a professional?" Yes It Can Be! Contact YesItCan.be
Powered by Movable Type 5.2.7