所见即所得的自动摘录

时间:2013-04-04 19:21:03

标签: wordpress wysiwyg

是否可以抓取WYSIWYG编辑器的内容并自动将前100个单词保存到摘录中?我知道excerpt_save_pre会在编辑器中保存摘录,但是没有看到任何可以从WYSIWYG编辑器中获取内容的内容。

1 个答案:

答案 0 :(得分:1)

我已经想到了这一点。保存/发布帖子时,“秘密”为&$_POST。这将构建一个数组,可以使用excerpt_save_pre提取内容,然后将其保存到摘录字段。

我进一步允许使用$length来控制字符数或字数,并控制输出取消注释的$output部分。

以下代码在我的vanilla网站上进行了测试。

function auto_insert_excerpt(){
$post_data = &$_POST;
$post_content = $post_data['content'];
$length = 15;

// This will return the first $length number of CHARACTERS
//$output = (strlen($post_content) > 13) ? substr($post_content,0,$length).'...' : $post_content;

// This will return the first $length number of WORDS
$post_content_array = explode(' ',$post_content);
if(count($post_content_array) > $length && $length > 0)
    $output = implode(' ',array_slice($post_content_array, 0, $length)).'...';

return $output;
}
add_filter('excerpt_save_pre', 'auto_insert_excerpt');