【问题标题】:Youtube embed and PHP displaying iframe but with display errorYoutube 嵌入和 PHP 显示 iframe 但显示错误
【发布时间】:2017-07-29 09:53:34
【问题描述】:

我正在为 wordpress 使用一个名为 gridby 的主题,当 fb_video 字段包含帖子编辑器中的链接并在博客提要页面中显示视频图像时,它使用自己的插件将特色图像更改为 vimeo 或 youtube 的 iframe和帖子中的 iframe 而不是特色图片。到目前为止,我只将它用于 vimeo 链接,它工作正常,今天只是尝试放入一个带有 /embed 的 youtube 视频链接,并且没有并且仍然显示错误。它不是我的代码,所以如果有人可以帮助我变得很棒,因为我需要这个功能,所以不想进一步刹车。我将 iframe 放入编辑器中的空白帖子中,显示效果很好,只是没有通过 PHP 代码显示。

/* ------------------------------------------------------------------------- *
 *  Video Functions
/* ------------------------------------------------------------------------- */ 

/*  display url image big youtube and vimeo
/* ------------------------------------ */

if ( !function_exists('video_image') ) {

    function video_image($url, $size){ 

        $image_url = parse_url($url);

        if($size == 'small') {

            if($image_url['host'] == 'www.youtube.com' || $image_url['host'] == 'youtube.com'){
                $array = explode("&", $image_url['query']);
                return "http://img.youtube.com/vi/".substr($array[0], 2)."/1.jpg";

            } else if($image_url['host'] == 'www.vimeo.com' || $image_url['host'] == 'vimeo.com'){
                $hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/".substr($image_url['path'], 1).".php"));
                return $hash[0]["thumbnail_small"];
            }

        } else if($size == 'hd') {

            if($image_url['host'] == 'www.youtube.com' || $image_url['host'] == 'youtube.com'){
                $array = explode("&", $image_url['query']);
                return "http://img.youtube.com/vi/".substr($array[0], 2)."/hqdefault.jpg";

            } else if($image_url['host'] == 'www.vimeo.com' || $image_url['host'] == 'vimeo.com'){
                $hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/".substr($image_url['path'], 1).".php"));
                return $hash[0]["thumbnail_large"];
            }
        }

    }

}

/*  display iframe of youtube and vimeo
/* ------------------------------------ */

if ( !function_exists('video_player') ) {

    function video_player($url, $autoplay){ 

        $image_url = parse_url($url);

        if($autoplay == '1') {

            if($image_url['host'] == 'www.youtube.com' || $image_url['host'] == 'youtube.com'){
                $array = explode("&", $image_url['query']);
                return "<iframe title='YouTube video player' width='400' height='275' src='http://www.youtube.com/embed/".substr($array[0], 2)."?wmode=trasparent&autoplay=1' frameborder='0' wmode='opaque' allowfullscreen></iframe>";

            } else if($image_url['host'] == 'www.vimeo.com' || $image_url['host'] == 'vimeo.com'){

                return "<iframe src='http://player.vimeo.com/video/".substr($image_url['path'], 1)."?autoplay=true' width='400' height='275' frameborder='0' allowfullscreen'></iframe>";
            }

        } else {

            if($image_url['host'] == 'www.youtube.com' || $image_url['host'] == 'youtube.com'){
                $array = explode("&", $image_url['query']);
                return "<iframe title='YouTube video player' width='400' height='275' src='http://www.youtube.com/embed/".substr($array[0], 2)."?wmode=trasparent' frameborder='0' wmode='opaque' allowfullscreen></iframe>";

            } else if($image_url['host'] == 'www.vimeo.com' || $image_url['host'] == 'vimeo.com'){

                return "<iframe src='http://player.vimeo.com/video/".substr($image_url['path'], 1)."' width='400' height='275' frameborder='0' allowfullscreen'></iframe>";
            }

        }

    }

}


/* ------------------------------------------------------------------------- *
 *  Filter Thumbnail
/* ------------------------------------------------------------------------- */ 

add_filter( 'post_thumbnail_html', 'post_thumbnail_html', 10, 3 );

    function post_thumbnail_html( $html, $post_id, $post_image_id) { 

        $video = get_post_meta( $post_id, 'fb_video', true );

        if ($html) { /* If have thumbnail image set */

            if ($video != '') {

                if (is_single()){ /* Show playe on single page */

                    if( function_exists('video_player')) { 

                    ?>

                        <div class='video-container'><?php echo video_player($video, 0); ?></div>

                    <?php 

                    }

                } else { 

                    echo '<i class="fa fa-play-circle fa-4x"></i>'. $html;

                }

            } else {

                echo $html;

            }

        } else { /* If haven't thumbnail image set */

            if ($video != '') {

                if (is_single()){ /* Show playe on single page */

                    if( function_exists('video_player')) { 

                    ?>

                        <div class='video-container'><?php echo video_player($video, 0); ?></div>

                    <?php 

                    }

                } else { 

                    echo '<i class="fa fa-play-circle fa-4x"></i><img class="img-res" src="'. video_image($video, 'hd').'" alt="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '" />';

                }

            } 

        }

    }



?>

【问题讨论】:

    标签: javascript php wordpress iframe youtube


    【解决方案1】:

    必须使用 /watch?v= 版本的 url 才能显示。我有一个与此相关的问题,仅要求用户将 youtube 的 ID 输入到自定义字段中会不会更容易?这样你就可以将 iframe 放入页面模板并使用 get meta 函数将其插入 / 之后?

    这就是我输入视频本身的方式,但我将如何为博客页面帖子获取视频图像?真的不需要插件我想在我自己的主题中使用此功能,因此寻找更清洁的替代方案

    【讨论】:

      【解决方案2】:

      https://wordpress.org/plugins/optimise-youtube-video-embed/

      此插件可以帮助您解决 iframe 问题。这将动态增加页面负载。单击按钮后,只会嵌入您的视频。

      在您的页面或帖子中包含短代码。您可以将自定义图像添加为视频横幅。

      [eh_optimize_youtube_embed video="youtube_link" banner="banner_image_link_optional"]
      

      【讨论】:

        猜你喜欢
        • 2020-01-26
        • 2014-03-20
        • 2012-08-29
        • 2019-06-20
        • 2018-08-02
        • 2016-10-21
        • 2020-12-09
        • 2023-03-29
        • 1970-01-01
        相关资源
        最近更新 更多