CMS multi-listing entry - can I show only snippet of content?

4 posts by 2 authors in: Forums > CMS Builder
Last Post: March 20, 2013   (RSS)

By meg - March 19, 2013

I'm using the CMS like a blog (multi-listing entries), and I'm interested in showing only a certain amount of characters of the content in the listing area. How do I do that?

So right now I'm using 

<?php foreach ($blog_entriesRecords as $record): ?>

<?php echo $record['content']; ?>

<?php endforeach ?>

to display the content. 

By dwellingproductions - March 19, 2013 - edited: March 19, 2013

Hi meg!

I'm working on a blog right now as well.  Here's how I accomplish what you're wanting to do.  First, use the following function:

<?php
// TEXT-LIMITING FUNCTION (to display only a portion of each blog post's content)
// For use on all Blog list pages

function cutText($string, $setlength) { 
    $length = $setlength; 
    if($length<strlen($string)){ 
        while (($string{$length} != " ") AND ($length > 0)) { 
            $length--; 
        } 
        if ($length == 0) return substr($string, 0, $setlength); 
        else return substr($string, 0, $length); 
    }else return $string; 

// END TEXT-LIMITING FUNCTION
?>

Then, to display it on your page do something like this:

<?php foreach ($blog_entriesRecords as $record): ?>
  <?php echo cutText($record['content'], 425); ?>&nbsp;... &nbsp;
  <a href="<?php echo $record['_link'] ?>" class="read_more">MORE >></a>
<?php endforeach ?>

The number in green is how many characters you want to display.  You would change this for your needs.  This is written so that it will always end on a complete word (so you don't get a couple letters of a word and then have it cut off, which can look really weird.)  :-)  The code shown in orange is optional.  But, I like adding a "MORE" link to help people continue reading the article.

Hope that helps!  :-)

- Jeremy

---------------------------

Dwelling Productions

www.dwellingproductions.com

By meg - March 20, 2013

Much appreciated!!! Works great - thank you!