Warning: Parameter 2 to qtranxf_postsFilter() expected to be a reference, value given in /home/.sites/609/site1266/web/blackbams-blog/wp-includes/class-wp-hook.php on line 286 Warning: count(): Parameter must be an array or an object that implements Countable in /home/.sites/609/site1266/web/blackbams-blog/wp-includes/post-template.php on line 284 Get the WordPress-excerpt by specifying a maximum amount of characters • excerpt get_the_excerpt the_excerpt • Blackbams Blog
15. May 2011
von Blackbam

A lot of sites want WordPress excerpts to have an accurate length to fit into a specified number of text lines. This can be required for design reasons, for example. The default WordPress excerpt length is managed by a given amount of words (default 70), which can be very imprecicsly when it comes to the actual length. Using the following function, you can get the excerpt by defining an desired amount of characters:

// define the desired amount of characters
$numberOfChars = 140;

// call the function
the_excerpt_max_charlength($numberOfChars);

// the function - copy this to the functions.php-file in your theme for example
function the_excerpt_max_charlength($charlength) {
   $excerpt = get_the_excerpt();
   $charlength++;
   if(strlen($excerpt)>$charlength) {
       $subex = substr($excerpt,0,$charlength-5);
       $exwords = explode(" ",$subex);
       $excut = -(strlen($exwords[count($exwords)-1]));
       if($excut<0) {
            echo substr($subex,0,$excut);
       } else {
       	    echo $subex;
       }
       echo "[...]";
   } else {
	   echo $excerpt;
   }
}

With a slight differentiation this function can not only be used for excerpt, but for a very general way to limit the words of a PHP string by specifying a maximum amount of characters. This is the function for general use:
// Shorten a String pattern to a maximum of characters without breaking words, by giving a String, maximum length and closing pattern if true
function cis_limit_words($pattern,$charlength=200,$after=" [...]") {
   $charlength++;
   if(strlen($pattern)>$charlength) {
       $subex = substr($pattern,0,$charlength-5);
       $exwords = explode(" ",$subex);
       $excut = -(strlen($exwords[count($exwords)-1]));
       if($excut<0) {
            echo substr($subex,0,$excut);
       } else {
            echo $subex;
       }
       echo $after;
   } else {
    echo $pattern;
   }
}
Because it proved to be useful I also provided this to the codex, improvements are welcome.
Share

Dieser Eintrag wurde am 15. May 2011 um 15:15 in der Kategorie WordPress, WP Scripts veröffentlicht. You can book the comments for this article RSS 2.0. Feedback, discussion, commendation and critics are welcome: Write a comment or trackback.


Tags: , ,

Warning: Use of undefined constant Ext_related_links - assumed 'Ext_related_links' (this will throw an Error in a future version of PHP) in /home/.sites/609/site1266/web/blackbams-blog/wp-content/themes/SilentWoodsByBlackbam/single.php on line 75

No comments yet

Kommentare abonnieren (RSS) or URL Trackback

Leave a comment: