【发布时间】:2015-11-14 17:46:23
【问题描述】:
tl;dr:我想从某个类别的帖子中提取嵌入的 Youtube 视频链接
我目前正在开发一个博客/文章网站。考虑以下内容:我有一个索引页面,其中包含 2 个精选视频的部分。假设我有一个查询和一个循环,可以从一个类别中检索 2 个最新帖子。此类别中的帖子始终以使用引导程序嵌入的精选视频开头:
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="..."></iframe>
</div>
<!-- some text content follows -->
目前我正在使用以下函数从帖子内容中检索文本摘录:
function get_excerpt_by_id($post_id){
$the_post = get_post($post_id); //Gets post ID
$the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
$excerpt_length = 30; //Sets excerpt length by word count
$the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
$the_excerpt = html_entity_decode($the_excerpt, ENT_QUOTES, 'UTF-8');
$words = explode(' ', $the_excerpt, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '…');
$the_excerpt = implode(' ', $words);
endif;
return $the_excerpt;
}
该函数从帖子中提取内容并提取前 30 个单词,同时剥离所有 html 标记和图像。 我怎么能反其道而行之?以某种方式检索嵌入的 YouTube 视频并摆脱其余部分?我有两个想法:
例如,我可以获取前 X 个字符,找到嵌入的结尾并删除其余部分。
-
我可以在帖子中添加一个特殊元素并将其中的所有内容都包含在内。类似的东西:
<span class="vid">...</span> 理论上,我可以只使用视频本身创建帖子,并且只使用 the_content(),但我想避免这种解决方案,因为只分享视频而对网站用户没有附加价值的网站有时会受到惩罚谷歌在搜索排名中。
我可以直接从嵌入的div中提取src元素。
哪种方法最好,或者有更好的方法吗?如果是这样,你能指出我正确的方向吗?
感谢您的建议。
【问题讨论】: