【问题标题】:auto excerpt from WYSIWYG所见即所得的自动摘录
【发布时间】:2013-04-04 19:21:03
【问题描述】:

是否可以抓取所见即所得编辑器的内容并将前 100 个单词自动保存到摘录中?我知道excerpt_save_pre 当您在编辑器中时它会保存摘录,但还没有看到任何可以从所见即所得编辑器中获取内容的东西。

【问题讨论】:

  • 您为什么要尝试从所见即所得编辑器中获取内容?当您保存帖子时,WordPress 可以自动创建具有特定长度的摘录(您可以在设置菜单中调整)。
  • 好点。我刚刚发现了一些使用 &$_POST 的东西,并且正在考虑从中获取内容。

标签: wordpress wysiwyg


【解决方案1】:

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

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

下面的代码在我的原版网站上进行了测试。

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');

【讨论】:

    猜你喜欢
    • 2014-08-21
    • 2011-07-18
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多