【问题标题】:Convert short-code to YouTube link in WordPress在 WordPress 中将短代码转换为 YouTube 链接
【发布时间】:2018-11-27 12:50:57
【问题描述】:

我需要转换这个简码:

[youtube id="ElWkGqhoDfE" width="720" height="380"]

到 Youtube 链接喜欢

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

所以,我需要在 WordPress AMP 中加载页面时对其进行转换

我该怎么做?

【问题讨论】:

    标签: wordpress youtube amp-html


    【解决方案1】:

    你可以使用这个功能

    function get_shortcode($code){
    if (strpos($code, 'youtube') !== false) {
        $youtube = explode(" ", $code);
        $youId = str_replace('"', "" ,str_replace('id="', "", $youtube[1]));
        $link = 'https://www.youtube.com/watch?v='.$youId ;
        return $link;
    }
    }
    $shortcode = '[youtube id="ElWkGqhoDfE" width="720" height="380"]' ;
    echo get_shortcode($shortcode);
    

    【讨论】:

    • 谢谢,,,但是内容中的简码,,如何检查内容
    • 你能把内容发给我吗?您可以使用 preg_match 函数查找短代码
    【解决方案2】:

    检查是否有效, 简单的做

    $shortcode = do_shortcode('[youtube id="ElWkGqhoDfE" width="720" height="380"]'); 
    echo $shortcode;
    

    现在 $shortcode 可供进一步使用

    【讨论】:

    • 显然您没有理解我的要求,我想将简码转换为链接,而不使用任何简码
    • 只需做$shortcode = do_shortcode('[youtube id="ElWkGqhoDfE" width="720" height="380"]'); echo $shortcode;
    • @sunil 的回答是正确的,请将问题改写为“WordPress:将从页面内容中提取的短代码转换为 YouTube 链接”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 2018-03-07
    • 1970-01-01
    • 2016-02-07
    • 2013-10-03
    • 2015-04-13
    • 2022-07-31
    相关资源
    最近更新 更多