首页 文章

如何以随机顺序显示帖子的图像 - Wordpress

提问于
浏览
0

我的wordpress中有一个页面,我通过“添加媒体”按钮添加了三个图像 .

现在,我正在寻找一种方法 display these images one at a time and in a random order .

只是猜测我必须调用页面的 the_content 并编写一些PHP以随机顺序一次显示一个图像?但由于我不知道如何编写这样的函数,一些帮助将是巨大的!

enter image description here

1 回答

  • 0

    你可以找到很多插件 . https://wordpress.org/plugins/tags/random-image/

    要么

    如果您使用自定义帖子类型,那么这可能对您有所帮助 .

    在foreach之前使用array_unique():

    <?php while ( have_posts() ) : the_post();
    
        $images = get_field('gallery');
    
        // thumbnail
        if( $images ): 
    ?>
        <ul id="container" class="tiles-wrap animated">
            <?php 
                $images = array_rand($images); 
                $images = array_unique($images);
    
                foreach( $images as $image ): 
    
                    // $rand_class = array('small', 'medium', 'large');
                    $size = 'medium';
                    $thumb = $image['sizes'][ $size ];
                    $width = $image['sizes'][ $size . '-width' ];
                    $height = $image['sizes'][ $size . '-height' ]; ?>
    
                   <li><img src="<?php echo $image['sizes']['medium']; ?>" alt="<?php echo $image['alt']; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" /></li>       
    
                <?php endforeach;
           endif; ?>
        </ul>
    <?php endwhile; ?>
    

相关问题