把get_content的变量放在array()中

时间:2015-05-23 13:21:40

标签: php arrays wordpress

拥有使用Vimeo拇指创建视频库的代码。

<?php
            $videos = Array("128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "128171814", "127958989", "126775227", "126775227", "126775227", "126775227", "126775227", "126775227", "126775227", "126775227", "126775227", "126775227", "126775227", "126775227", "126775227", "126775227", "126775227", "126775227", "126775227",);
        ?>

        <div class="video-responsive">

        <iframe src="https://player.vimeo.com/video/<?= $videos[0] ?>" width="500" height="309" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="display: block; margin-bottom: 10px"></iframe>

        </div>
        <div class="thumbs">
        <div>
        <?php
            foreach($videos as $video)
            {
                $hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$video.php"));
        ?><img src="<?= $hash[0]['thumbnail_medium'] ?>" onclick="openvideo(<?= $video ?>)"><?php
            }
        ?>
        </div>
        <span id="prev"><</span>
        <span id="next">></span>
        </div><!-- thumbs -->

脚本,它创建拇指

<script>
            function openvideo(video)
            {
            document.querySelector("iframe").src = "https://player.vimeo.com/video/" + video;
            }
        </script>

        <script>
        Array.prototype.forEach.call(document.querySelectorAll(".thumbs div"), function($div)
                         {
                             $div.style.width = document.querySelectorAll(" img").length * 100 / 4 + "px";
                         });
document.querySelector("#next").onclick = function()
{
var i = 100;
var intervalId = setInterval(function()
            {
                document.querySelector(".thumbs").scrollLeft += 1;
                if(i == 0)
                {
                    clearInterval(intervalId);
                }
                i--;
            });
};
document.querySelector("#prev").onclick = function()
{
var i = 100;
var intervalId = setInterval(function()
            {
                document.querySelector(".thumbs").scrollLeft -= 1;
                if(i == 0)
                {
                    clearInterval(intervalId);
                }
                i--;
            });
};
        </script>

$videos = Array("128171814",... - 其ID视频。 这在array直接写入ID时可以正常工作,但我希望在wordpress post编辑器中输入ID。

在Wordpress中的网站,我创建了自定义帖子类型,在编辑器中放置了"128171814", "128171814", "128171814",,我想在数组中显示()

现在代码看起来像

<? $args = array(
'post_type' => 'video-gallery',
    );?>

    <?php query_posts($args); ?>

    <?php if (have_posts()) : ?>

    <?php while (have_posts()) : the_post(); ?>

        <?php
        $videocontent = get_the_content();
            $videos = Array($videocontent);
        ?>

.....ALL CODE .....

<?php endwhile; ?>
    <?php endif; ?> 
    <?php wp_reset_query()  ?> 

但现在iframe中的链接看起来像

<iframe src="https://player.vimeo.com/video/" 128171814",="" "128171814",="" "128171814","=""></iframe>

请告诉我正确的方法。 抱歉我的英语不好。 谢谢。

1 个答案:

答案 0 :(得分:1)

以下位有问题。函数get_the_content()返回逗号分隔值字符串。所以你需要用逗号分割它并创建数组。

$videocontent = get_the_content(); // Outputs: string(51) ""128171814", "128171814", "128171814", "128171814"
$videos = Array($videocontent);

所以改变上面这一点:

$videocontent = get_the_content();
// Spit the comma separated values
$videos = explode(',', trim($videocontent, ','));

// Trim out the spaces and double quote
foreach($videos as $key => $val) {
    $videos[$key] = trim($val, ' "');
}