dsn1327 2019-03-21 23:49
浏览 44

从youtube嵌入缩略图和视频(php - Wordpress)

I would like to find out the php code where you able to embed the thumbnail from a youtube video to a website and when you clic on the thumbnail the video starts to play. All of this one using a shortcode (php) to add within of the post and typing into this shortcode the Video ID (got from Youtube) that you want to post. I have tried the follow code but it does not work and not include all my needs shown above.

Finally, I would like to join the shortcode shows below with [video] shortcode available for Wordpress, because I want to change the video player to the built-in WordPress player, which doesn't have the familiar Youtube styling.

Also, I searched the internet and I did not find all neccesary information

May you help me? Thanks in advance.

PHP Code:

function mininaturas_youtube($atts) {
    extract(shortcode_atts(array(
         'id' => '',
         'img' => '0',
         'align'=>'left'
    ), $atts));
   $align_class='align'.$align;
   return '<img src="http://img.youtube.com/vi/'.$id.'/'.$img.'.jpg" alt="" class="'.$align_class.'" />';
}
add_shortcode('miniatura_youtube', 'miniaturas_youtube');

To be added within of the post:

[miniatura_youtube id="3Jt7-nBfULU" img="0" align="center"]
  • 写回答

1条回答 默认 最新

  • dounai6626 2019-03-22 04:57
    关注

    I was able to do something with an iframe, like so:

    function mininaturas_youtube_func( $atts ) {
        $a = shortcode_atts( array(
            'id' => 'dQw4w9WgXcQ'
        ), $atts);
        return '<iframe width="560" height="315" src="https://www.youtube.com/embed/' . $a["id"] . '" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
    }
    add_shortcode( 'miniatura_youtube', 'mininaturas_youtube_func' );
    

    You can add your other parameters in the same manner as 'id' if you need them.

    As it is noted here https://codex.wordpress.org/Shortcode_API :

    'id' => 'dQw4w9WgXcQ' just represents the default id if no other id is provided

    Hope this helps!

    Edit Also just for clarity I used the same short code as you

    [miniatura_youtube id="XXXXXXXXXXX"]

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)