-
Notifications
You must be signed in to change notification settings - Fork 24
excerpt
Miika Arponen edited this page Jul 5, 2016
·
1 revision
Add this to your functions.php
to get a helper that you can use to shorten strings. It has one mandatory parameter, string
that is the string to shorten. The string won't be cut from just anywhere, but from the last word-break before the cutting point.
It can also be given a length
parameter that is the number of characters that is used to cut the text. By default the length is 140.
class Excerpt extends \DustPress\Helper {
public function output() {
if ( ! isset( $this->params->string ) ) {
return "DustPress Excerpt helper error: no string defined.";
}
if ( ! is_string( $this->params->string ) ) {
return "DustPress Excerpt helper error: string is not a string.";
}
else {
$string = preg_replace( "/\r|\n/", "", $this->params->string );
}
if ( isset( $this->params->length ) ) {
$length = $this->params->length;
}
else {
$length = 140;
}
if ( strlen( $string ) <= $length ) {
return $string;
}
else {
return substr( $string, 0, strpos( wordwrap( $string, $length ), "\n" ) );
}
}
}
dustpress()->add_helper( 'excerpt', new Excerpt() );