新闻 | 循环不同的col-md

我知道 wordpress 循环是这样完成的:


  <?php

    /*MOSTRA NEWS CPT ALLENAMENTO*/

    $query_mostra_cpt_allenamento = array(

        'post_type' => 'news-allenamento',

        'posts_per_page'    => 4,

        'orderby' => 'meta_value',

        'order' => 'ASC',

    );

    $res_cpt_allenamento = new WP_Query( $query_mostra_cpt_allenamento );

    if( $res_cpt_allenamento->have_posts() ) : ?>

        <div style="padding-top: 30px;" class="row">


            <?php  while( $res_cpt_allenamento->have_posts() ) : $res_cpt_allenamento->the_post(); ?>

                <!-- Grid row -->

                <div style="padding-bottom: 10px;padding-top: 10px;" class="col-md-6">

                    ...

                </div>

                <!-- Grid column -->

            <?php endwhile; ?>

        </div>


    <?php endif;

    wp_reset_postdata();

    ?>

我将有 4 个帖子col-md-6


现在我需要更多东西,或者更确切地说,我想创建一个自动结构,但是,它是以这种方式组成的,我该怎么做?


<div class="container">

    <div class="row">

        <div style="padding-right: 0px;padding-left: 0px;border: 1px solid #fff" class="col-md-6">

            <div class="view">

                ...

            </div>

        </div>

                <div style="padding-right: 0px;padding-left: 0px;border: 1px solid #fff" class="col-md-6">

                    <div class="container-fluid">

                        <div class="row">

                            <div style="padding-right: 0px;padding-left: 0px;border: 1px solid #fff" class="col-md-6">

                                <div class="view">

                                    ...

                                </div>

                            </div>

                        </div>

                    </div>

                </div>

    </div>

</div>

那会给我这个结果:新闻杂志


我怎样才能用wordpress循环制作这个结构?


LEATH
浏览 97回答 1
1回答

呼啦一阵风

一个选项是像这样拆分查询:<?php&nbsp; &nbsp; /*MOSTRA NEWS CPT ALLENAMENTO*/&nbsp; &nbsp; $query_mostra_cpt_allenamento = array(&nbsp; &nbsp; &nbsp; &nbsp; 'post_type' => 'news-allenamento',&nbsp; &nbsp; &nbsp; &nbsp; 'posts_per_page' => 1, // return only the first post&nbsp; &nbsp; &nbsp; &nbsp; 'orderby' => 'meta_value',&nbsp; &nbsp; &nbsp; &nbsp; 'order' => 'ASC',&nbsp; &nbsp; &nbsp; &nbsp; 'no_found_rows' => true,&nbsp; &nbsp; );&nbsp; &nbsp; /*MOSTRA NEWS CPT ALLENAMENTO*/&nbsp; &nbsp; $query_mostra_cpt_allenamento_after = array(&nbsp; &nbsp; &nbsp; &nbsp; 'post_type' => 'news-allenamento',&nbsp; &nbsp; &nbsp; &nbsp; 'posts_per_page' => 3,&nbsp; &nbsp; &nbsp; &nbsp; 'offset' => 1,// start from the second post&nbsp; &nbsp; &nbsp; &nbsp; 'orderby' => 'meta_value',&nbsp; &nbsp; &nbsp; &nbsp; 'order' => 'ASC',&nbsp; &nbsp; &nbsp; &nbsp; 'no_found_rows' => true,&nbsp; &nbsp; );&nbsp; &nbsp; $res_cpt_allenamento = new WP_Query( $query_mostra_cpt_allenamento );&nbsp; &nbsp; $res_cpt_allenamento_after = new WP_Query( $query_mostra_cpt_allenamento_after ); ?>&nbsp; &nbsp;<div style="padding-top: 30px;" class="row">&nbsp; &nbsp; <?php if( $res_cpt_allenamento->have_posts() ) : ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php while( $res_cpt_allenamento->have_posts() ) : $res_cpt_allenamento->the_post(); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div style="padding-right: 0px;padding-left: 0px;border: 1px solid #fff" class="col-md-6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="view">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endwhile; ?>&nbsp; &nbsp; <?php endif;&nbsp; &nbsp; wp_reset_postdata();&nbsp; &nbsp; ?>&nbsp; &nbsp; <?php if( $res_cpt_allenamento_after->have_posts() ) : ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div style="padding-right: 0px;padding-left: 0px;border: 1px solid #fff" class="col-md-6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="container-fluid">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php while( $res_cpt_allenamento_after->have_posts() ) : $res_cpt_allenamento_after->the_post(); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div style="padding-right: 0px;padding-left: 0px;border: 1px solid #fff" class="col-md-6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="view">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endwhile; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; <?php endif;&nbsp; &nbsp; wp_reset_postdata();?>&nbsp; &nbsp; </div>另一种选择是在你的 while 循环中添加一个计数器,检查你在哪里并采取相应的行动:<?php&nbsp; &nbsp; /*MOSTRA NEWS CPT ALLENAMENTO*/&nbsp; &nbsp; $query_mostra_cpt_allenamento = array(&nbsp; &nbsp; &nbsp; &nbsp; 'post_type' => 'news-allenamento',&nbsp; &nbsp; &nbsp; &nbsp; 'posts_per_page'&nbsp; &nbsp; => 4,&nbsp; &nbsp; &nbsp; &nbsp; 'orderby' => 'meta_value',&nbsp; &nbsp; &nbsp; &nbsp; 'order' => 'ASC',&nbsp; &nbsp; );&nbsp; &nbsp; $res_cpt_allenamento = new WP_Query( $query_mostra_cpt_allenamento );&nbsp; &nbsp; $i = 0;&nbsp; &nbsp; if( $res_cpt_allenamento->have_posts() ) : ?>&nbsp; &nbsp; &nbsp; &nbsp; <div style="padding-top: 30px;" class="row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; while( $res_cpt_allenamento->have_posts() ) : $res_cpt_allenamento->the_post(); $i++; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php if ($i < 2) : ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div style="padding-right: 0px;padding-left: 0px;border: 1px solid #fff" class="col-md-6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="view">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div style="padding-right: 0px;padding-left: 0px;border: 1px solid #fff" class="col-md-6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="container-fluid">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php else : ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div style="padding-right: 0px;padding-left: 0px;border: 1px solid #fff" class="col-md-6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="view">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endwhile; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; <?php endif;&nbsp; &nbsp; wp_reset_postdata();&nbsp; &nbsp; ?>不确定结构,但这是逻辑
打开App,查看更多内容
随时随地看视频慕课网APP