A
A
aex-svs2021-06-24 13:10:22
WordPress
aex-svs, 2021-06-24 13:10:22

Layout stretch. How to display 2 articles in 1 slide?

Good afternoon!
I'm integrating the layout in wordpress and now I'm faced with the difficulty of displaying articles in the slider.
The question is the following is necessary to display 2 articles in 1 slide. In my opinion, it is necessary to do it through iteration, but this is the difficulty, a huge request to tell me, I go in circles for 2 days)) Thanks in advance
Accordingly, if I put all posts (-1), all posts are displayed in one slide

<div class="blog">
            <div class="container">
                <h2 class="blog-title">Блог и статьи</h2>
            </div>
            <div class="line-trhought"></div>
            <div class="container">

            <div class="owl-carousel owl-theme">
            <div class="slide">
            
      <?php       
                $posts = get_posts( array(
                  'numberposts' => 2,
                  'category'    => 0,
                  'orderby'     => 'date',
                  'order'       => 'DESC',
                  'include'     => array(),
                  'exclude'     => array(),
                  'meta_key'    => '',
                  'meta_value'  =>'',
                  'post_type'   => 'post',
                  'suppress_filters' => true, // подавление работы фильтров изменения SQL запроса
                ) );

                foreach( $posts as $post ){
                  setup_postdata($post);
                   ?>
                    <div class="blog-item">
                  <div class="blog-img-wrapper">
                    <img class="blog-item-img" src="<?php echo get_the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>" >
                    <div class="blog-item-cover"><p class="blog-item-title"><?php the_title(); ?></p>
                    </div>
                  </div>
                  
                  <div class="blog-item-content">
                    <p><?php the_excerpt(); ?></p>
                    <a class="btn" href="<?php the_permalink(); ?>">Узнать больше</a>
                  </div>
                </div>   
                               
                <?php
                }
                wp_reset_postdata(); // сброс
                  ?> 
                                    
  </div> 
    </div>
  </div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pLavrenov, 2021-06-24
@aex-svs

Option 1:
You get, for example, 30 articles, then you need to divide it into slides of 2, for this, at the end of each iteration in foreach, you check the index for parity and if it is even, then display the closing tag of the slide and the opening tag of the next slide.
Option 2:
array-chunk break the original array of articles into chunks and first foreach chunks into slides and then in each slide foreach into articles.
In this option, it will be more convenient if, for example, there is a need to get the number of articles in a slide from metadata (for example, if the template allows you to select not 2 in a slide, but 3 or 4 for example)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question