【问题标题】:Retrieving an embeded YouTube video from a wordpress post从 wordpress 帖子中检索嵌入的 YouTube 视频
【发布时间】: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 视频并摆脱其余部分?我有两个想法:

  1. 例如,我可以获取前 X 个字符,找到嵌入的结尾并删除其余部分。

  2. 我可以在帖子中添加一个特殊元素并将其中的所有内容都包含在内。类似的东西:

    <span class="vid">...</span> 
    
  3. 理论上,我可以只使用视频本身创建帖子,并且只使用 the_content(),但我想避免这种解决方案,因为只分享视频而对网站用户没有附加价值的网站有时会受到惩罚谷歌在搜索排名中。

  4. 我可以直接从嵌入的div中提取src元素。

哪种方法最好,或者有更好的方法吗?如果是这样,你能指出我正确的方向吗?

感谢您的建议。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我可以给你完成的代码,但你自己试试吧,你以后可能会需要这个。

    您应该使用 DOM,并且可以使用正则表达式提取视频 ID。 例如:

    https://www.youtube.com/watch?v=EuQLMXyGQOE
    

    ID 是EuQLMXyGQOE

    http://php.net/manual/en/book.dom.php

    http://php.net/manual/en/function.preg-match.php

    编辑:如果你卡住了,这里是完成的代码 http://pastebin.com/FhV5yQTV

    【讨论】:

    • 谢谢你,我会努力解决的:)
    【解决方案2】:

    使用第一个答案中的资源以及更多研究我做了这个函数:

     <?php
            $args = array(
                'category_name'  => 'featured-video',
              'posts_per_page' => '2'
            );
            query_posts($args);      
            if (have_posts()) : while (have_posts()) : the_post(); 
    
            $content = $post->post_content;
            $doc = new DOMDocument();
            @$doc->loadHTML($content);
            $iframes = $doc->getElementsByTagName('iframe');
            foreach ($iframes as $frame) {
                   echo '<div class="embed-responsive embed-responsive-16by9">        
                          <iframe class="embed-responsive-item" src="'.$frame->getAttribute('src').'" frameborder="0" allowfullscreen></iframe>
                        </div><br>';
            }
    
          endwhile; endif;    
            wp_reset_query();
          ?>   
    

    【讨论】:

      【解决方案3】:

      从 3.6.0 版开始,WordPress 引入了 get_media_embedded_in_content() 函数来轻松获取嵌入内容。

      $media = get_media_embedded_in_content(
          apply_filters( 'the_content', get_the_content() ), 'video'
      );
      
      print_r($media);
      
      // results
      [
          0 => '<iframe>content</iframe>,
      ]
      

      支持的音频类型:“audio”、“video”、“object”、“embed”或“iframe”。

      您可以在这里找到更多信息https://developer.wordpress.org/reference/functions/get_media_embedded_in_content/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-02
        • 2013-12-19
        • 1970-01-01
        • 2016-01-19
        相关资源
        最近更新 更多