2 循环与随机和相同的帖子 WordPress [英] 2 loop with random and same post WordPress

查看:22
本文介绍了2 循环与随机和相同的帖子 WordPress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个带有 orderby random 的循环(一切正常),但我想为每个循环显示相同的帖子,示例:我发布了 1 到 10 个帖子;循环 1 显示帖子 1 2 和 3;循环 2 需要显示相同的帖子 1 2 和 3,我的代码是:

<div class="carousel slide" data-ride="carousel" id="quote-carousel"><!-- 底部轮播指示器--><ol class="carousel-indicators"><?php$args = 数组('post_type' =>'推荐','orderby' =>'兰','posts_per_page' =>3);$loop = new WP_Query( $args );$i = 0;while ( $loop->have_posts() ) : $loop->the_post();$avatar_testimonials = get_field('avatar-testimonials');?><li data-target="#quote-carousel" data-slide-to="<?php echo $i; ?>"class="<?php if ($i == 0) echo 'active'; ?>"><img class="img-responsive" alt="<?php echo the_title(); ?>"src="<?php if ($avatar_testimonials) {echo $avatar_testimonials['url'];} else {the_post_thumbnail_url('thumbnail');} ?>"alt=""></li><?php $i++;终了;?></ol><!-- 旋转木马幻灯片/行情--><div class="carousel-inner text-center"><?php$args = 数组('post_type' =>'推荐','orderby' =>'兰','posts_per_page' =>3);$loop = new WP_Query( $args );$i = 0;while ( $loop->have_posts() ) : $loop->the_post();?><!-- 引用 1 --><div class="item <?php if ($i == 0) echo 'active'; ?>"><blockquote><div class="row"><div class="col-sm-8 col-sm-offset-2"><?php echo the_excerpt();?><small><a href="<?php echo the_permalink(); ?>"title="<?php echo the_title(); ?>"><?php echo the_title();?></a></small>

</blockquote>

<?php $i++;终了;wp_reset_postdata();?>

解决方案

试试这个,我认为它对你有用.

<div class="carousel slide" data-ride="carousel" id="quote-carousel"><!-- 底部轮播指示器--><ol class="carousel-indicators"><?php$args = 数组('post_type' =>'推荐','orderby' =>'兰','posts_per_page' =>3);$loop = new WP_Query( $args );$i = 0;while ( $loop->have_posts() ) : $loop->the_post();$avatar_testimonials = get_field('avatar-testimonials');?><li data-target="#quote-carousel" data-slide-to="<?php echo $i; ?>"class="<?php if ($i == 0) echo 'active'; ?>"><img class="img-responsive" alt="<?php echo the_title(); ?>"src="<?php if ($avatar_testimonials) {echo $avatar_testimonials['url'];} else {the_post_thumbnail_url('thumbnail');} ?>"alt=""></li><?php $i++;终了;?></ol><!-- 旋转木马幻灯片/行情--><div class="carousel-inner text-center"><?php$i = 0;while ( $loop->have_posts() ) : $loop->the_post();?><!-- 引用 1 --><div class="item <?php if ($i == 0) echo 'active'; ?>"><blockquote><div class="row"><div class="col-sm-8 col-sm-offset-2"><?php echo the_excerpt();?><small><a href="<?php echo the_permalink(); ?>"title="<?php echo the_title(); ?>"><?php echo the_title();?></a></small>

</blockquote>

<?php $i++;终了;wp_reset_postdata();?>

I have two loop with orderby random(all work good), but I want to show same post for each loop, example: I have post 1 to 10; loop 1 show post 1 2 and 3; loop 2 need to show same post 1 2 and 3, my code is:

<div class="col-md-12" data-wow-delay="0.2s">
        <div class="carousel slide" data-ride="carousel" id="quote-carousel">
        <!-- Bottom Carousel Indicators -->
                <ol class="carousel-indicators">
<?php
$args = array( 
'post_type' => 'testimonials', 
'orderby' => 'rand', 
'posts_per_page' => 3 );
$loop = new WP_Query( $args );
$i = 0;
while ( $loop->have_posts() ) : $loop->the_post();

$avatar_testimonials = get_field('avatar-testimonials');
?>               
                    <li data-target="#quote-carousel" data-slide-to="<?php echo $i; ?>" class="<?php if ($i == 0) echo 'active'; ?>"><img class="img-responsive" alt="<?php echo the_title(); ?>" src="<?php if ($avatar_testimonials) {echo $avatar_testimonials['url'];} else {the_post_thumbnail_url('thumbnail');} ?>" alt=""></li>                
<?php $i++; endwhile;  ?>
                </ol>
                <!-- Carousel Slides / Quotes -->
                <div class="carousel-inner text-center">
<?php
$args = array( 
'post_type' => 'testimonials', 
'orderby' => 'rand', 
'posts_per_page' => 3 );
$loop = new WP_Query( $args );
$i = 0;
while ( $loop->have_posts() ) : $loop->the_post();
?>

                                <!-- Quote 1 -->
                    <div class="item <?php if ($i == 0) echo 'active'; ?>">
                        <blockquote>
                            <div class="row">
                                <div class="col-sm-8 col-sm-offset-2">
                                        <?php echo the_excerpt(); ?>
                                    <small><a href="<?php echo the_permalink(); ?>" title="<?php echo the_title(); ?>"><?php echo the_title(); ?></a></small>
                                </div>
                            </div>
                        </blockquote>
                    </div>
<?php $i++; endwhile;  wp_reset_postdata(); ?>
                </div>
        </div>
    </div>

解决方案

Try this, I think it will work for you.

<div class="col-md-12" data-wow-delay="0.2s">
    <div class="carousel slide" data-ride="carousel" id="quote-carousel">
        <!-- Bottom Carousel Indicators -->
        <ol class="carousel-indicators">
            <?php
            $args = array( 
            'post_type' => 'testimonials', 
            'orderby' => 'rand', 
            'posts_per_page' => 3 );

            $loop = new WP_Query( $args );
            $i = 0;
            while ( $loop->have_posts() ) : $loop->the_post();

            $avatar_testimonials = get_field('avatar-testimonials');
            ?>               
                <li data-target="#quote-carousel" data-slide-to="<?php echo $i; ?>" class="<?php if ($i == 0) echo 'active'; ?>"><img class="img-responsive" alt="<?php echo the_title(); ?>" src="<?php if ($avatar_testimonials) {echo $avatar_testimonials['url'];} else {the_post_thumbnail_url('thumbnail');} ?>" alt=""></li>                
            <?php $i++; endwhile;  ?>
        </ol>

        <!-- Carousel Slides / Quotes -->
        <div class="carousel-inner text-center">
            <?php

            $i = 0;
            while ( $loop->have_posts() ) : $loop->the_post();
            ?>

            <!-- Quote 1 -->
            <div class="item <?php if ($i == 0) echo 'active'; ?>">
                <blockquote>
                    <div class="row">
                        <div class="col-sm-8 col-sm-offset-2">
                                <?php echo the_excerpt(); ?>
                            <small><a href="<?php echo the_permalink(); ?>" title="<?php echo the_title(); ?>"><?php echo the_title(); ?></a></small>
                        </div>
                    </div>
                </blockquote>
            </div>
            <?php $i++; endwhile;  wp_reset_postdata(); ?>

        </div>
    </div>
</div>

这篇关于2 循环与随机和相同的帖子 WordPress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆