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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗