2013-04-04 100 views
1

是否有可能抓住所見即所得編輯器的內容並自動將前100個單詞保存到摘錄中?我知道excerpt_save_pre可以在編輯器中保存摘錄,但沒有看到任何可以從WYSIWYG編輯器獲取內容的內容。自動摘錄所見即所得

+0

你爲什麼試圖抓住所見即所得的編輯器的內容?當你保存你的文章時,WordPress可以自動創建一個特定長度的摘錄(你可以在設置菜單中調整)。 – MeRuud 2013-04-04 19:25:10

+0

好點。我剛剛發現了一些使用'&$ _ POST'的內容,並且正在從中獲取內容。 – Pat 2013-04-04 19:29:21

回答

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