【问题标题】:Advanced Custom Field - Displaying a youtube video on a Woocommerce template高级自定义字段 - 在 Woocommerce 模板上显示 youtube 视频
【发布时间】:2015-03-19 01:33:02
【问题描述】:

我已经设置了一个自定义字段,您可以通过 wordpress 中的文本小部件在其中添加 youtube 链接的结尾,然后一个函数将其添加到 url 中的 iframe。

下面是添加这个到页面的函数:

                <?php if (($videoUrl = get_field('video_name', $post->ID)) != '') {
            //Extract the url arguments (everything after the '?'
            if (($query = parse_url($videoUrl, PHP_URL_QUERY)) != '') {
                //Convert the arguments into an array for each access
                parse_str($query, $arguments);
                if (array_key_exists('v', $arguments)) {
                    //Create the embed code using the id from the 'v' argument
                    $videoId = $arguments['v'];
                    ?>
            <div class="videoContainer">
                <iframe width="390" height="219" src="https://www.youtube.com/embed/<?= $videoId ?>?rel=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>
            </div>
            <?php 
        }
    }
} ?>

我已将其添加到内容中,但似乎仍然没有显示任何内容。有人可以帮我找出任何错误..

【问题讨论】:

  • $videoId 返回什么
  • 检查 iframe 的 src url 并匹配,是否是预期的 youtube url...

标签: php wordpress youtube advanced-custom-fields


【解决方案1】:

使用高级自定义字段在 wordpress 模板中显示 YouTube 视频

1) 在 acf 中创建文本字段。 示例:文本字段名称为 video_url

2) 请在 acf 文本字段中输入 youtube 视频链接。

3) 在wordpress模板页面的任何地方显示

<?php // use inside loop
echo $youtubevideo_code = wp_oembed_get( get_field('video_url') );
?>

【讨论】:

    猜你喜欢
    • 2018-08-26
    • 2021-06-07
    • 2016-09-07
    • 2016-11-13
    • 2018-09-17
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 2019-10-10
    相关资源
    最近更新 更多