在 wordpress 循环的结果周围包裹一个 div,但不包括前 2 个

我有一个自定义帖子类型的循环。我为每个帖子带回了一个标题、图像和内容块。我想将光滑的滑块应用于结果以创建光滑的轮播,但我不想包含循环的前两个结果 - 所以我需要为结果创建一个父 div 但只在之后启动该 div前两个结果。


我已经尝试了在循环计数上查询结果的方法,以将一个类仅应用于前两个结果,但这并没有真正实现我所追求的。


<div class="wrapper_for_news_items">

  <?php 


$posts = get_posts(array(

    'posts_per_page'    => -1,

    'post_type'         => 'news',

    'order' => 'DESC'

));


if( $posts ): ?>

    <?php $post = $posts[0]; $c=0; ?>




    <?php foreach( $posts as $post ): 


        setup_postdata( $post );


        ?>


    <div class="treatment_block news_block <?php $c++; if($c == 1) { echo ' featured'; } elseif($c == 2) { echo ' featured'; } ?>">

    <h2  class="block_title above"> <?php the_title( '' ); ?></h2>

     <h3 class="post_date top">

      <?php echo get_the_date() ?>

    </h3>

      <div class="post_icon" style="background-image: url('<?php 

if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.

    the_post_thumbnail_url($post_id, 'thumbnail');

?>');">

      <button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>

      </div>

            <h2  class="block_title below"> <?php the_title( '' ); ?></h2>

    <h3 class="post_date bottom">

      <?php echo get_the_date() ?>

    </h3>

            <p class="excerpt">

            <?php the_excerpt( '' ); ?>

            </p>


      </div>




    <?php endforeach; ?>



    <?php wp_reset_postdata(); ?>


                           <?php else : ?>


                        No News Found!



<?php endif; ?>



<!-- end of news loop -->

            </div> <!-- treatment news block wrapper -->


UYOU
浏览 128回答 1
1回答

开满天机

您可以只创建 2 个循环。将第一个用于特色输出,第二个用于轮播。<div class="wrapper_for_news_items">&nbsp; <?php&nbsp;$args_with_two_posts = array(&nbsp; &nbsp; 'posts_per_page'&nbsp; &nbsp; => 2,&nbsp; &nbsp; 'post_type'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'news',&nbsp; &nbsp; 'order'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'DESC');$query_with_two_posts = new WP_Query( $args_with_two_posts );if( $query_with_two_posts->have_posts ) :&nbsp;&nbsp; while ( $query_with_two_posts->have_posts ) : $query_with_two_posts->the_posts; ?>&nbsp; <div class="treatment_block news_block featured">&nbsp; &nbsp; <h2 class="block_title above">&nbsp; &nbsp; &nbsp; <?php the_title( '' ); ?>&nbsp; &nbsp; </h2>&nbsp; &nbsp; <h3 class="post_date top">&nbsp; &nbsp; &nbsp; <?php echo get_the_date() ?>&nbsp; &nbsp; </h3>&nbsp; &nbsp; <div class="post_icon" style="background-image: url('<?php&nbsp;if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.&nbsp; &nbsp; the_post_thumbnail_url($post_id, 'thumbnail');}&nbsp;?>');">&nbsp; &nbsp; &nbsp; <button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>&nbsp; &nbsp; </div>&nbsp; &nbsp; <h2 class="block_title below">&nbsp; &nbsp; &nbsp; <?php the_title( '' ); ?>&nbsp; &nbsp; </h2>&nbsp; &nbsp; <h3 class="post_date bottom">&nbsp; &nbsp; &nbsp; <?php echo get_the_date() ?>&nbsp; &nbsp; </h3>&nbsp; &nbsp; <p class="excerpt">&nbsp; &nbsp; &nbsp; <?php the_excerpt( '' ); ?>&nbsp; &nbsp; </p>&nbsp; </div>&nbsp; <?php endwhile; ?>&nbsp; <?php wp_reset_postdata(); ?>&nbsp; <?php else : ?> No News Found!&nbsp; <?php endif; ?>&nbsp; <!-- end of 2 post initial news loop --></div><!-- treatment news block wrapper --><?php&nbsp; // Start your second loop containing the slickslider content&nbsp;&nbsp;?><div class="wrapper_for_news_carousel_items">&nbsp; <?php&nbsp;$args_with_all_posts = array(&nbsp; &nbsp; 'posts_per_page'&nbsp; &nbsp; => -1,&nbsp; &nbsp; 'offset'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 2 // Offset the 2 initial posts&nbsp; &nbsp; 'post_type'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'news',&nbsp; &nbsp; 'order'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'DESC');$query_with_two_posts = new WP_Query( $args_with_all_posts );if( $args_with_all_posts->have_posts ) :&nbsp;&nbsp; while ( $args_with_all_posts->have_posts ) : $args_with_all_posts->the_posts; ?>&nbsp; <div class="treatment_block news_block">&nbsp; &nbsp; <h2 class="block_title above">&nbsp; &nbsp; &nbsp; <?php the_title( '' ); ?>&nbsp; &nbsp; </h2>&nbsp; &nbsp; <h3 class="post_date top">&nbsp; &nbsp; &nbsp; <?php echo get_the_date() ?>&nbsp; &nbsp; </h3>&nbsp; &nbsp; <div class="post_icon" style="background-image: url('<?php&nbsp;if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.&nbsp; &nbsp; the_post_thumbnail_url($post_id, 'thumbnail');}&nbsp;?>');">&nbsp; &nbsp; &nbsp; <button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>&nbsp; &nbsp; </div>&nbsp; &nbsp; <h2 class="block_title below">&nbsp; &nbsp; &nbsp; <?php the_title( '' ); ?>&nbsp; &nbsp; </h2>&nbsp; &nbsp; <h3 class="post_date bottom">&nbsp; &nbsp; &nbsp; <?php echo get_the_date() ?>&nbsp; &nbsp; </h3>&nbsp; &nbsp; <p class="excerpt">&nbsp; &nbsp; &nbsp; <?php the_excerpt( '' ); ?>&nbsp; &nbsp; </p>&nbsp; </div>&nbsp; <?php endwhile; ?>&nbsp; <?php wp_reset_postdata(); ?>&nbsp; <?php else : ?> No News Found!&nbsp; <?php endif; ?>&nbsp; <!-- end of news loop --></div><!-- treatment news carousel items -->或者您可以计算循环中的帖子并在第三个帖子之前和最后一个帖子之后指定一个包装器以创建轮播。<div class="wrapper_for_news_items">&nbsp; <?php&nbsp;$args_with_two_posts = array(&nbsp; &nbsp; 'posts_per_page'&nbsp; &nbsp; => 2,&nbsp; &nbsp; 'post_type'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'news',&nbsp; &nbsp; 'order'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'DESC');$query = new WP_Query( $args_with_two_posts );$counter = 1; // Set the counterif( $query->have_posts ) :&nbsp;&nbsp; while ( $query->have_posts ) : $query->the_posts;&nbsp;&nbsp;&nbsp;&nbsp; if ( $count == 3 ) { echo '<div class="slick-slider">'; };&nbsp; ?>&nbsp; <div class="treatment_block news_block">&nbsp; &nbsp; <h2 class="block_title above">&nbsp; &nbsp; &nbsp; <?php the_title( '' ); ?>&nbsp; &nbsp; </h2>&nbsp; &nbsp; <h3 class="post_date top">&nbsp; &nbsp; &nbsp; <?php echo get_the_date() ?>&nbsp; &nbsp; </h3>&nbsp; &nbsp; <div class="post_icon" style="background-image: url('<?php&nbsp;if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.&nbsp; &nbsp; the_post_thumbnail_url($post_id, 'thumbnail');}&nbsp;?>');">&nbsp; &nbsp; &nbsp; <button class="post__link but" rel="<?php the_ID(); ?>">READ MORE</button>&nbsp; &nbsp; </div>&nbsp; &nbsp; <h2 class="block_title below">&nbsp; &nbsp; &nbsp; <?php the_title( '' ); ?>&nbsp; &nbsp; </h2>&nbsp; &nbsp; <h3 class="post_date bottom">&nbsp; &nbsp; &nbsp; <?php echo get_the_date() ?>&nbsp; &nbsp; </h3>&nbsp; &nbsp; <p class="excerpt">&nbsp; &nbsp; &nbsp; <?php the_excerpt( '' ); ?>&nbsp; &nbsp; </p>&nbsp; </div>&nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $counter++; // Add +1 every loop&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (($query->current_post +1) == ($query->post_count)) {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '</div>'; // This is the last post&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; endwhile;&nbsp;&nbsp; ?>&nbsp; <?php wp_reset_postdata(); ?>&nbsp; <?php else : ?> No News Found!&nbsp; <?php endif; ?>&nbsp; <!-- end of news loop --></div><!-- treatment news block wrapper -->
打开App,查看更多内容
随时随地看视频慕课网APP