首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在col-4和col-8之间交替的<div>中包装博客文章

在col-4和col-8之间交替的<div>中包装博客文章
EN

Stack Overflow用户
提问于 2019-07-15 21:01:43
回答 2查看 80关注 0票数 0

我正在设置一个博客页面布局,其中我需要交替显示col4和col8列,而不是手动进入每个列并获取所需的帖子。

我尝试在col4中添加循环,但是我不知道如何在col4和col8之间进行切换。

代码语言:javascript
复制
    <?php
    global $wpdb;

    $args = array(
      'post_type' => 'post',
      'orderby' => 'date',
      'order' => 'DESC',
      'nopaging' => true,
    );

    $this_query = new WP_Query( $args );

    ?>
     <section class="ftco-section ftco-project" id="projects-section">
        <div class="container-fluid">
            <div class="row justify-content-center pb-5">
          <div class="col-md-12 heading-section text-center ftco-animate">
              <h1 class="big big-2">Projects</h1>
            <h2 class="mb-4">Our Projects</h2>
          </div>
        </div>
            <div class="row">
            <?php if ( $this_query->have_posts() ) : ?>
        <?php while ( $this_query->have_posts() ) : $this_query->the_post(); ?> 
                <div class="col-md-4">
                    <div class="project img ftco-animate d-flex justify-content-center align-items-center" style="background-image: url('<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>');">
                        <div class="overlay"></div>
                        <div class="text text-center p-4">
                            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                            <span>Web Design</span>
                        </div>
                    </div>
                  </div>
                  <div class="col-md-8">
                    <div class="project img ftco-animate d-flex justify-content-center align-items-center" style="background-image: url('<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>');?>/images/project-5.jpg);">
                        <div class="overlay"></div>
                        <div class="text text-center p-4">
                            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                            <span>Web Design</span>
                        </div>
                    </div>
                  </div>
                  <?php endwhile; 
                  wp_reset_postdata();
                  else : ?>
                  <p><?php esc_html_e( 'Sorry, no posts found.' ); ?></p>
                  <?php endif; ?>
                </div>
              </div>
            </section>

我希望col-4和col-8自动交替填充。我尝试过这样做,但是博客文章并没有在col4和col8之间交替,而是只填充了col4 div中的所有博客文章。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-15 21:19:03

试试这段代码。

代码语言:javascript
复制
<?php $class = "col-md-4"; // 2nd element class ?> 
<?php while ( $this_query->have_posts() ) : $this_query->the_post(); ?> 
<?php
     if($class == "col-md-4") { $class = "col-md-8"; }
    else if ($class == "col-md-8") { $class = "col-md-4"; }

?>
                <div class="<?php echo $class; ?>">
                    <div class="project img ftco-animate d-flex justify-content-center align-items-center" style="background-image: url('<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>');">
                        <div class="overlay"></div>
                        <div class="text text-center p-4">
                            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                            <span>Web Design</span>
                        </div>
                    </div>
                  </div>
                  <?php endwhile; 
票数 2
EN

Stack Overflow用户

发布于 2019-07-15 21:20:17

代码语言:javascript
复制
$x=0;

if ( $this_query->have_posts() ) : 
    while ( $this_query->have_posts() ) : $this_query->the_post();
        $class_array = array('col-4', 'col-8');
        $col_class = $class_array[$x%2];
        $x++;

    <div class="<?php echo $col_class ?>"></div>

    endwhile;
   wp_reset_postdata();
endif; 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57040323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档