我需要一个很好的解决方案来声明基于POST计数的两个变量的值

时间:2017-02-21 作者:semidivine

我用foreach循环显示了三篇文章(自定义文章类型),每一篇都显示了一个音频播放器和一些元数据。音频通过JW播放器播放,我使用他们的api调用它。JW播放器代码中有一个函数,允许您在按下一个播放器时阻止任何其他播放器播放。

这是整个循环。它确实起作用,但我想要更优雅、更有活力的东西:

<?php 
    $posts = get_posts(array(
        \'numberposts\' => 3,
        \'post_type\' => \'audio\'
    ));
    if( $posts ): 
    $i = 1; 
?>
<?php foreach( $posts as $post ) : ?>
    <div class="media-container audio player-<?php echo $i; ?>">
        <div id="player-<?php echo $i; ?>">Loading this Audio...</div>
<?php 
if ($i == 1) :
    $x1 = 2;
    $x2 = 3;
elseif ($i == 2) :
    $x1 = 1;
    $x2 = 3;
elseif ($i == 3) :
    $x1 = 1;
    $x2 = 2;
endif;
?>
        <script type="text/javaScript">
            var playerInstance = jwplayer("player-<?php echo $i; ?>");
            playerInstance.setup({
                file: \'<?php the_field("audio_upload"); ?>\',
                image: \'<?php the_field("audio_image"); ?>\',
                events:{
                    onPlay: function() {
                        jwplayer(\'player-<?php echo $x1; ?>\').stop();
                        jwplayer(\'player-<?php echo $x2; ?>\').stop();
                    }
                }
            });
        </script>

        <div class="media-details">
            <h2 class="audio-title"><?php the_field(\'name_of_audio\'); ?>    </h2>
        </div>
    </div>
<?php $i++; endforeach; wp_reset_postdata(); endif; ?>
我想知道是否有一种方法可以动态地获取$x1和$x2,而不是像我这样显式地声明值。例如,如果我决定在这个页面上显示4或5(或10)个帖子,我希望不必更改$x1和$x2的代码。任何帮助都将不胜感激。

2 个回复
最合适的回答,由SO网友:David Lee 整理而成

你需要一秒钟for 将输出stop function 对于除当前玩家以外的所有玩家,如下所示:

<?php

$posts = get_posts(array(
    \'numberposts\' => 3,
    \'post_type\' => \'any\'
));

$number_of_posts_returned = count($posts); //because it can be less than the ones we wanted

if ($posts):
    $i = 1;
    ?>
    <?php foreach ($posts as $post) : ?>
        <div class="media-container audio player-<?php echo $i; ?>">
            <div id="player-<?php echo $i; ?>">Loading this Audio...</div>
            <script type="text/javaScript">
                var playerInstance = jwplayer("player-<?php echo $i; ?>");
                playerInstance.setup({
                file: \'<?php //the_field("audio_upload"); ?>\',
                image: \'<?php //the_field("audio_image"); ?>\',
                events:{
                    onPlay: function() {
                        <?php 
                        //lets create the stop call for all except the current number
                        for ($x = 1; $x <= $number_of_posts_returned; $x++) { 
                            if($x != $i){
                       ?>
                            jwplayer(\'player-<?php echo $x; ?>\').stop();

                      <?php }//end IF

                        } //end for
                       ?>

                    }
                }
                });
            </script>

            <div class="media-details">
                <h2 class="audio-title"><?php //the_field(\'name_of_audio\'); ?>    </h2>
            </div>
        </div>
        <?php $i++;
    endforeach;
    wp_reset_postdata();
endif; ?>

SO网友:moraleida

这里有一个简单的解决方案,它只需要从当前加载的玩家那里获取ID。

首先,为玩家div添加一个公共类:

<div id="player-<?php echo $i; ?>" class="jwplayer-loaded">Loading this Audio...</div>

然后在脚本中,获取所有当前加载的玩家,并逐个停止他们,但当前玩家除外:

<script type="text/javaScript">
    // don\'t forget to esc_js() any values you print to javascript code
    // https://developer.wordpress.org/reference/functions/esc_js/
    var playerId = "player-<?php echo esc_js( $i ); ?>",
        playerInstance = jwplayer( playerId );
    playerInstance.setup({
        file: \'<?php the_field("audio_upload"); ?>\',
        image: \'<?php the_field("audio_image"); ?>\',
        events:{
            onPlay: function() {
                var playerList = document.querySelectorAll(\'.jwplayer-loaded\');

                [].forEach.call( playerList, function( v ) {
                    if ( v.id !== playerId ) {
                        jwplayer( v.id ).stop();
                    }
                } );
            }
        }
    });
</script>

相关推荐

Where is this inline CSS code

CSS代码在哪里?我是说margin-top: 46px !important!;我需要把它改成1px 去掉上边距。但我没有在任何主题的文件中找到它。Note: I have searched the texts in all files using FileSeek Pro. 但什么也没发现。(即使Firefox中有Inspect元素)