javascript - YouTube嵌入和PHP显示iframe但显示错误

标签 javascript php wordpress iframe youtube

当fb_video字段在帖子编辑器中包含链接并在博客供稿页面和iframe中显示视频图像时,我正在使用一个名为gridby for wordpress的主题,该主题使用其自己的插件将特色图片更改为vimeo或youtube的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 ) ) . '" />';

                }

            } 

        }

    }



?>

最佳答案

必须使用网址的/ watch?v =版本才能使其显示。我有一个与此有关的问题,问用户在自定义字段中输入youtube的ID是否会更容易?这样,您可以将iframe放入页面模板中,并使用get meta函数将其插入/后面。

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

关于javascript - YouTube嵌入和PHP显示iframe但显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42677962/

相关文章:

javascript - 根据其他表单字段填写选择选项输入

javascript - 如何停止在 wordpress 短代码中多次排队相同的脚本

html - 图像对齐 - float 后间距不正确

javascript - jQuery 淡入/淡出文本,然后淡入新文本

javascript - 异常后的清理(如 Eloquent JavaScript 中所述)

javascript - 根据 JSON 数据使用 jQuery 定位元素

php - 在 For 循环内爆

javascript - 通过 URL 传递字符串数组值的最佳方法

javascript - 在 JS 中验证 secureimage 验证码

php mysql 更新错误