Creating Blog Summary text on blogList.php

3 posts by 2 authors in: Forums > CMS Builder
Last Post: September 7, 2011   (RSS)

Hello,

I have started creating my blog using the Blog Section Editor and things are looking good :)

I have setup the "List page" (blogList.php) to display all of my blog entries and this is working fine. However, I would like to have the content of each blog post displayed on blogList.php limited to, for example, 100 characters, rather than have all of the content displayed. Is there any way to do this?

I would then have a "read more" link that directs the user to the blogDetail.php page if the user wants to read all of the text in the blog post.

Thanks,
Greg

Re: [gversion] Creating Blog Summary text on blogList.php

By Jason - September 7, 2011

Hi Greg,

You can use a function that has been developed called maxWords. You give this function your content and the maximum number of words you want displayed, and it returns the shortened text.

Here is the function:

<?php

function maxWords($textOrHtml, $maxWords) {

$text = strip_tags($textOrHtml, "<b></b><i></i>");
$words = preg_split("/\s+/", $text, $maxWords+1);

if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);

return $output;
}

?>


You can put this somewhere on your blogList.php page. To keep your code neat, it's a good idea to put functions down at the bottom of the page.

You can then call the function like this:

Example:
<?php echo maxWords($record['content'], 20); ?>

This code will print the first 20 words of the content field to the screen.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/