我的目标是定义自定义摘录长度。我知道我可以在函数中定义一个新函数。php位于二十一世纪的子主题中。
它应覆盖以下行:
/**
* Sets the post excerpt length to 40 words.
*
* To override this length in a child theme, remove the filter and add your own
* function tied to the excerpt_length filter hook.
*/
function twentyeleven_excerpt_length( $length ) {
return 40;
}
add_filter( \'excerpt_length\', \'twentyeleven_excerpt_length\' );
签署人:
function new_excerpt_length($length) {
return 20;
}
add_filter(\'excerpt_length\', \'new_excerpt_length\');
但它什么也没做。即使我将默认值40更改了另一个值。
有什么想法吗?
SO网友:Marc Wiest
这允许您单独设置每个标记的长度。
// nouveller.com/quick-tips/quick-tip-8-limit-the-length-of-the_excerpt-in-wordpress/
function custom_excerpt_length($string, $word_limit) {
// creates an array of words from $string (this will be our excerpt)
// explode divides the excerpt up by using a space character
$words = explode(\' \', $string);
// this next bit chops the $words array and sticks it back together
// starting at the first word \'0\' and ending at the $word_limit
// the $word_limit which is passed in the function will be the number
// of words we want to use
// implode glues the chopped up array back together using a space character
return implode(\' \', array_slice($words, 0, $word_limit));
}
function custom_excerpt_more($more) {
global $post;
return get_permalink($post->ID);
}
add_filter(\'excerpt_more\', \'custom_excerpt_more\');
// Call Function Like This:
// echo custom_excerpt_length(get_the_excerpt(), \'25\');