Back to Projects

Truncate

  • on 20/12/2010 (updated on 25/03/2014)
  • in plugins, wolf.

This plugin allows you to make automated excerpts on your site with simple call. It is a simple function and can go into snippet if you don’t want to call it like a plugin, but this way it’s clutter free and works like a charm :).

Advantages of this plugin:

  • all excerpts are made automatically,
  • it recognizes some basic html tags like p, a, br, img etc and closes them if text is longer then number of characters you defined,
  • it cuts by word, not characters so it won’t cut your word in half,
  • you can customize your readmore link with css.

Installation

This one is classic Wolf:

  1. download plugin
  2. unzip it under “/wolf/plugins/” directory
  3. rename its folder to “truncate”
  4. go to Administration->Plugins
  5. enable plugin

Simple as that!

Usage

You have to call function truncate where you want to make your excerpts and pass it to variable. Function itself can take these arguments:

  • your content,
  • number of characters,
  • suffix.
<?php truncate(CONTENT, LIMIT, SUFFIX); ?>

If you want to disable “suffix” then you just add “” (empty quotes) on third place.

Basically, you call it like this:

<?php
$myarticles = truncate($article->content(), 250, "..."); 
echo $myarticles;
?>

Example code is used for /articles tree

1. Simple example (default):

<?php $last_articles = $this->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<?php foreach ($last_articles as $article): ?>
<div class="entry">
<h3><?php echo $article->link($article->title); ?></h3>
<?php
$myarticles = truncate($article->content(), 300, "...");
echo $myarticles;
?>
</div>
<?php endforeach; ?>

And this is how your excerpts will look like by default:

Default truncate

2. With Read more link

<?php $last_articles = $this->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<?php foreach ($last_articles as $article): ?>
<div class="entry">
<h3><?php echo $article->link($article->title); ?></h3>
<?php
$myarticles = truncate($article->content(), 300, "");  
echo $myarticles;
echo '<p>'.$article->link('Read more...').'</p>';
?>
</div>
<?php endforeach; ?>

The “p” is here just for readability and can be styled differently with css.

And it looks like this:

Truncate with read more link

Changelog

Version 0.0.2

  • fixed problems with “Strict Standard…” (thanks to Tina)

Version 0.0.1

  • initial release