我对WordPress完全陌生,所以很容易:)
我在模板中输入以下代码:
<?php excerpt(20);?>
这样做是用20个字来限制文本。我现在想知道是否有类似的功能限制字符而不是字?
谢谢!
答案 0 :(得分:2)
我用这个:
add_filter('excerpt_length', 'my_excerpt_length');
function my_excerpt_length($length) {
return '500';
}
function better_excerpt($limit, $id = '') {
global $post;
if($id == '') $id = $post->ID;
else $id = $id;
$postinfo = get_post($id);
if($postinfo->post_excerpt != '')
$post_excerpt = $postinfo->post_excerpt;
else
$post_excerpt = $postinfo->post_content;
$myexcerpt = explode(' ', $post_excerpt, $limit);
if (count($myexcerpt) >= $limit) {
array_pop($myexcerpt);
$myexcerpt = implode(' ',$myexcerpt).'...';
} else {
$myexcerpt = implode(' ',$myexcerpt);
}
$myexcerpt = preg_replace('`\[[^\]]*\]`','',$myexcerpt);
$stripimages = preg_replace('/<img[^>]+\>/i', '', $myexcerpt);
return $stripimages;
}
然后在我的主题文件中,我只是用它来调用它:
better_excerpt('50') //50 being how many words I want
也适用于自定义插件/小部件。
答案 1 :(得分:1)
我在我的functions.php中使用它:
function truncate ($str, $length=10, $trailing='...'){
// take off chars for the trailing
$length-=mb_strlen($trailing);
if (mb_strlen($str)> $length){
// string exceeded length, truncate and add trailing dots
$str = mb_substr($str,0,$length);
$str = explode('. ',$str);
for( $i=0; $i<(sizeof($str)-2); $i++ ):
$newstr .= $str[$i].". ";
endfor;
return $newstr;
} else{
// string was already short enough, return the string
$res = $str;
}
return $res;
}
它应该截断为字符计数,但然后进一步截断到截断之前的最后一个句点。当您的摘录包含链接或其他标记时,它确实会出现问题 - 换句话说,最好在帖子中使用摘录字段而不是使用此功能自动摘录,因为您不能在摘录字段中使用HTML
答案 2 :(得分:1)
Wordpress不支持摘录方法的字符分隔符,有一个名为Advanced Excerpt的插件。安装完成后,您可以拨打the_advanced_excerpt('length=20&use_words=0')
答案 3 :(得分:0)
请使用此代码限制发布内容...
<a href="<?php the_permalink(); ?>"><?php substr($post->post_content, 0, xy); ?> ...</a>
更改XY的限制....