Multiple Excerpt Lengths in Wordpress

Multiple excerpt lengths in wordpress

How about...

function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);

if (count($excerpt) >= $limit) {
array_pop($excerpt);
$excerpt = implode(" ", $excerpt) . '...';
} else {
$excerpt = implode(" ", $excerpt);
}

$excerpt = preg_replace('`\[[^\]]*\]`', '', $excerpt);

return $excerpt;
}

function content($limit) {
$content = explode(' ', get_the_content(), $limit);

if (count($content) >= $limit) {
array_pop($content);
$content = implode(" ", $content) . '...';
} else {
$content = implode(" ", $content);
}

$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);

return $content;
}

then in your template code you just use..

<?php echo excerpt(25); ?>

from: http://bavotasan.com/tutorials/limiting-the-number-of-words-in-your-excerpt-or-content-in-wordpress/

Multiple Wordpress excerpt lengths on single page

See this question and answer. There's a function that let's you specify how the length of the excerpt on an ad-hoc basis.

Wordpress: How to obtain different excerpt lengths depending on a parameter

uhm, answering me again, the solution was actually quite trivial. it's not possible, as far as i know, to pass a parameter to the function my_excerpt_length() (unless you want to modify the core code of wordpress), but it is possible to use a global variable. so, you can add something like this to your functions.php file:

function my_excerpt_length() {
global $myExcerptLength;

if ($myExcerptLength) {
return $myExcerptLength;
} else {
return 80; //default value
}
}
add_filter('excerpt_length', 'my_excerpt_length');

And then, before calling the excerpt within the loop, you specify a value for $myExcerptLength (don't forget to set it back to 0 if you want to have the default value for the rest of your posts):

<?php
$myExcerptLength=35;
echo get_the_excerpt();
$myExcerptLength=0;
?>


Related Topics



Leave a reply



Submit