问题描述
function custom_excerpt_length( $length ) { return 32; } add_filter( 'excerpt_length','custom_excerpt_length',999 );
当有人创建博客文章时,我需要限制出现在 BuddyPress 活动流上的字数。目前,当有人创建新的博客文章时,摘录会出现在活动流中,大约有 50 多个字。我需要将其限制在 32 个字左右。
有谁知道如何做到这一点?
谢谢
解决方法
这是我正在使用的。第一部分限制文本,第二部分确保缩略图存在。感谢 shanebp 为我指明了正确的方向。
function 5_activity_content_body( $content,$activity ) {
$content_length = 42;
if ( 'new_blog_post' == $activity->type ) {
$images = extractImageFromContent($content);
$_content = (strlen($content) > $content_length) ? substr($content,$content_length).'...' : $content;
if(!empty($images)) {
$_content .= implode("",$images);
}
return $_content;
}
return $content;}
add_filter( 'bp_get_activity_content_body','5_activity_content_body',10,2 );
这会带回缩略图。
function extractImageFromContent($html) {
preg_match_all('/<img.*?src=[\'"](.*?)[\'"].*?>/i',$html,$matches);
return ($matches[0]) ? $matches[0] : array(); }