这非常适合我使用它的目的(WordPress的自动元描述和og:description内容),但我想知道是否有一种方法可以把它写得更短/更干净:
$content = $post -> post_content;
$content = wp_trim_words($content, 40, '...'); // 40 words
$content = trim(str_replace(' ','',$content));
$content = do_shortcode($content);
$content = html_entity_decode($content);
$content = strip_tags($content);
$content = preg_replace('/'s's+/', '', $content);
已更新
好吧,我想这就行了。我想能够一次又一次地使用它,然后我想,嘿,也许它就像js。我是一个头脑中的视觉设计师,所以我需要一些时间才能掌握这些东西的窍门。
function do_meta_description_cab() {
global $post;
$content = $post -> post_content;
$content = wp_trim_words($content, 40, '...'); // 40 words
$content = trim(str_replace(' ','',$content));
$content = do_shortcode($content);
$content = html_entity_decode($content);
$content = strip_tags($content);
$content = preg_replace('/'s's+/', '', $content);
return $content;
}
//Usage: $content = do_meta_description_cab();
要剥离所有html打开和关闭标记,可以使用:
$content = preg_replace('/<[^>]+>|<'/[^>]+>/', '', $content);