如何显示带有col-12的前四个记录,并且重置记录将以col-8(col-6和col-6)之类的内容显示

我正在主页上显示最近发布的帖子,这些帖子正在WordPress网站上显示。现在,我必须在引导栏中显示记录,如下图所示。

如何显示带有col-12的前四个记录,并且重置记录将以col-8(col-6和col-6)之类的内容显示

图片说明:

1)前四行将显示第12行。

2)然后我必须将第8列和第4列分开。

3)在第8列中,我必须在第6-6列中显示我的记录的重置。

4)在第4列中,我必须显示一些不是来自数据库的静态文本。

所以我的HTML代码将显示如下。

<div class="container">
        <div class="row">

            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12"></div>
            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12"></div>
            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12"></div>
            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12"></div>
            <div class="row">
                <div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 col-xs-12">
                    <div class="row">
                        <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-xs-12"></div>
                        <div class="col-xl-6 col-lg-6 col-md-8 col-sm-12 col-xs-12"></div>
                        <!-- and so on -->
                    </div>
                </div>
                <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12">
                    <!-- html text this is not apart of for each-->
                </div>
            </div>

        </div>
    </div>

我的预期输出是

如何显示带有col-12的前四个记录,并且重置记录将以col-8(col-6和col-6)之类的内容显示

我对如何执行此操作感到困惑。目前,我正在第12列中获取所有记录。下面是我的代码。

function recentPost_on_home2(){
ob_start();
?>
<div class="cp-seeWrapper">
    <div class="row">
   <?php 
    global $paged;
    $paged = $paged ? $paged : get_query_var( 'page' );
    $args = array('posts_per_page' => 6,'paged'=> $paged,);
    $tyler_query = new WP_Query( $args );
    if ($tyler_query->have_posts()) {

               while ( $tyler_query->have_posts() ) {
                    $tyler_query->the_post();
                    $names = array();
                    $categories = get_the_category();
                    foreach ( $categories as $i => $term ) {
                        if ( $i < 1 ) { // show at most two
                            $names[] = '<a href="' . esc_url( get_category_link( $term->term_id ) ) . '">' . $term->name . '</a>';
                        }
                    }
                    $names = implode( '',$names );
                    ?>
                <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
                    <div class="cp-shadow cp-seeSinglePostWrapper">
                        <a href="<?php echo esc_url( get_the_permalink() );?>" title="<?php echo esc_attr( the_title_attribute('echo=0'));?>" class=""><?php echo get_the_post_thumbnail();?></a>
                        <div class="bg-white single-post-box">
                            <div class="d-flex cp-CategoryList">
                                <div class="seeDate"><?php echo get_the_date('F j,Y');?></div>
                                <div class="cp_cat_list"><?php echo $names;?></div>
                            </div>
                            <div class="cp-b-content">
                                <h2><a href="<?php echo esc_url( get_the_permalink() );?>" title="<?php echo esc_attr( the_title_attribute('echo=0'));?>" class=""><?php echo wp_trim_words(get_the_title(),12,'...');?></a>
                                </h2>
                            </div>
                                <p><?php echo wp_trim_words(get_the_excerpt(),25,'...');?></p>
                        </div>
                    </div>
                </div>
            <?php  }?>

    <?php   }?>

    <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12">
                <div class="cp-social cp-shadow bg-white">
                <h3>Social</h3>
            </div>
    </div>

    <div class="cp-postPagination">
         <div class="nav-previous cp-pagi float-left"><?php previous_posts_link(); ?></div>
         <div class="nav-next cp-pagi float-right"><?php next_posts_link( null,$tyler_query->max_num_pages ); ?> </div>
          <div class="clearfix"></div>
    </div>
</div>
</div>
    <?php return ob_get_clean(); 
}
add_shortcode( 'home_recent_post2','recentPost_on_home2' );
cfclx 回答:如何显示带有col-12的前四个记录,并且重置记录将以col-8(col-6和col-6)之类的内容显示

您可以尝试这个伴侣:

编辑

<div class="container">
  <div class="row">
  …
  …
  …
  $index = 0;
  foreach ( $categories as $i => $term ) {
    if ($index < 3) {
      <div class="col-12">…</div>
    } else {
      <div class="col-12 col-sm-4">…</div>
      <div class="col-12 col-sm-4">…</div>
      <div class="col-auto">…</div>
    }
    $index++;
  }
  …
  </div>
</div>

我讲逻辑,您的工作用php / html语法!祝你好运:)

别忘了:

  • col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12col-12完全相同(请选中此Bootstrap grid options
  • row然后再将col再将row再插入,以此类推是非常不正确的做法,只是先放置一个 row然后再放置{ {1}},然后关闭您的col
,

最后,我找到了解决方案。我不知道这是不是正确的方法,但是它解决了我的问题。

我只是分享代码示例

我做了什么, 我在循环外添加了containerrow这两个类,并在while循环之前声明了两个名为$index = 0; $check=0;的php变量。

然后我在循环和条件为if-else内的内部添加了if ($index < 4)条件。为什么选择4,因为我必须在第12行中显示前4行,所以我要在前12行中显示前4行。

然后在else条件下,我添加了

if ($check==0){?>
<div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 col-xs-12"><div class="row">
<?php $check=1;}?>

为什么要使用上面的代码,因为上面的代码仅在一次while循环中工作一次,因此由8列创建。 在其他关闭之后,我添加了$index++;以增加记录。

就是这样。我得到了预期的输出。

<div class="container">
  <div class="row">
    <?php if ($tyler_query->have_posts()) {
      $index = 0; 
      $check=0;
      while ( $tyler_query->have_posts() ) {
      if ($index < 4) {?>
    <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
      <!--content-->
    </div>
    <?php  } else {              
           if ($check==0){?>
    <div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 col-xs-12">
      <div class="row">
        <?php $check=1;}?>
        <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-xs-12">
          <!--content-->
        </div>
        <?php   } $index++;}?>
      </div>
    </div>

    <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12">
      <!-- html text this is not apart of for each-->
    </div>

  </div>
</div>
本文链接:https://www.f2er.com/3138983.html

大家都在问