doujie4787 2019-03-26 21:35
浏览 31
已采纳

由Rand wp_query订购

enter image description here

I used two wp query by rand from one category for two colums left (into two posts) and right (one post), sometimes coincides posts, how i can use one wp query by rand and add class for left colum for adding class flex-direction: colum ?

<div class="info__block__main d-flex">
    <div class="small__info__block">
        <div class="child__info__block d-flex">             
            <?php
            $project_cat = array(1,7,33,38,43,40,35,45);
            $select_post_1 = get_field('select_post_from_services_small');

            $args = array(
                'paged' => $paged,
                'cat' => $project_cat,
                'post_type' => 'post',
                'posts_per_page' => 2,
                'post__in' => $select_post_1,
                'orderby'  => 'rand'
            );

            $query = new WP_Query($args);
            if($query->have_posts()) :
                while($query->have_posts()) :
                    $query->the_post();
                    get_template_part('parts/project', 'parts2');
                endwhile;
                wp_reset_query();
            endif;
            ?>
        </div>
    </div>
    <div class="big__info__block">
        <?php
        $project_cat = array(1,7,33,38,43,40,35,45);
        $select_post = get_field('select_post_from_services_big');

        $args = array(
            'paged' => $paged,
            'cat' => $project_cat,
            'post_type' => 'post',
            'posts_per_page' => 1,
            'post__in' => $select_post,
            'orderby'  => 'rand'
        );

        $query = new WP_Query($args);
        if($query->have_posts()) :
            while($query->have_posts()) :
                $query->the_post();
                get_template_part('parts/project', 'parts1');
            endwhile;
            wp_reset_query();
        endif;
        ?>
    </div>
</div>  

I try add count into loop, but result unsuccess, maybe i cant right clear code.

  • 写回答

1条回答 默认 最新

  • duandu1915 2019-03-27 20:48
    关注

    If nobody helps me, i fix this problem via this code ;)

            $i = 1;
            echo '<div class="c__block">';
            if($query->have_posts()) :
                while($query->have_posts()) :
                    $query->the_post();
                    if($i % 2 == 0) {echo '</div><div class="c__block_1">';}
                    get_template_part('parts/project', 'parts2');
                $i++; endwhile;
                echo '</div>';
                wp_reset_query();
            endif;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化