Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Problems with links when using $Content.LimitWordCountXML(xx)

Go to End

5 Posts   1960 Views


Community Member, 19 Posts

16 May 2013 at 12:13am


is there a possibility to parse out links when using "$Content.LimitWordCountXML(xx)" in my template? I use the blog module ( Silverstripe 3 ) on my website and "$Content.LimitWordCountXML(18)" in to show some short teasers on the BlogHolder page. The problem is: when I have a link in my BlogEntry content like

" <a href="">Screenplane</a> "

it will be displayed like this on my website:

" Screenplane[] "

Can somebody help me with this issue?

Many thanks,


Community Member, 189 Posts

16 May 2013 at 12:28am

Hi iraira88

You'll have to create a custom function for your BlogEntry class to get the functionality you want. Start by creating a DataExtension of the BlogEntry class.

I've included some code I used in an old 2.4 project. it is untested in 3.0 so may require some modification.

class BlogEntryExtension extends DataExtension {

	 * Returns a Summary of the content but retains links (including the pesky site_tree_id links)
	 * also removes images.
	 * @return (string)
	public function getCustomSummary($maxWords = 35){
		$content = $this->obj('Content');
		$data = (string)$content->forTemplate();
		$data = strip_tags($data, '<a>');
		// grab the first paragraph, or, failing that, the whole content
		if( strpos( $data, "\n\n" ) )
			$data = substr( $data, 0, strpos( $data, "\n\n" ) );
		$sentences = explode( '.', $data );	
		$count = count( explode( ' ', $sentences[0] ) );
		// if the first sentence is too long, show only the first $maxWords words
		if( $count > $maxWords ) {
			return implode( ' ', array_slice( explode( ' ', $sentences[0] ), 0, $maxWords ) ).'...';
		// add each sentence while there are enough words to do so
		$result = '';
		do {
			$result .= trim(array_shift( $sentences )).'.';
			if(count($sentences) > 0) {
				$count += count( explode( ' ', $sentences[0] ) );
			// Ensure that we don't trim half way through a tag or a link
			$brokenLink = (substr_count($result,'<') != substr_count($result,'>')) ||
				(substr_count($result,'<a') != substr_count($result,'</a'));
		} while( ($count < $maxWords || $brokenLink) && $sentences && trim( $sentences[0] ) );
		if( preg_match( '/<a[^>]*>/', $result ) && !preg_match( '/<\/a>/', $result ) )
			$result .= '</a>';
		return $result;


and then in your _config.php add


and in use $CustomSummary instead of $Content.LimitWordCountXML. Hope that helps.


Community Member, 19 Posts

16 May 2013 at 4:19am

Thank you very much IOTI!

I've included your "getCustomSummary" function to my BlogEntry class just to see if it works for me (and $getCustomSummary in Everything works great now! :)

Do I still need to do it with a BlogEntryExtension? Is there any difference between both solutions?


Community Member, 189 Posts

16 May 2013 at 4:48am

That's fantastic it worked ! :)

You don't have to use a DataExtension class but it is highly recommended. The reason being, when a new blog module update comes out and you go to update your blog module, you'll have to account for that change in the code every time. You'll have to manually update the BlogEntry.php class with the getCustomSummary() function. Using a DataExtension, you won't have to do this!

You'll want to make a new php file in your mysite/code folder called BlogEntryExtension.php and use the code i provided in my previous post. After you make the new BlogEntryExtension.php class and add the extra line to your _config.php do a ?flush=all on your site. it won't take long and will save you a ton of time in the future.


Community Member, 1 Post

15 May 2017 at 3:53pm

If it's not being used for XML you can just use LimitWordCount() and links will work fine.