vic4884
11/22/2018 - 2:27 PM

WP_Query и get_posts

сравнение

//get_posts();
<div>
              <?php
                  $args = [
                    'post_type' => 'business_sercvice',
                    'posts_per_page' => 3,
                    //'offset' => 3, //- после какого поста выводить 
                    'order' => 'ASC'
                  ]; 
                  $custom_posts = get_posts( $args ); ?>
                  <?php foreach( $custom_posts as $post ) : setup_postdata($post); ?>
                      <div class="grid_4">
                         <?php the_post_thumbnail(); ?>
                         <h3><?php the_title(); ?></h3>
                         <?php the_content(); ?>
                         <a href="<?php the_permalink(); ?>" class="btn">Read more</a>
                      </div>
                  <?php endforeach; wp_reset_postdata(); ?>
              </div>

//WP_Query();
<?php
                $args = [
                  'post_type' => 'business_sercvice',
                  'posts_per_page' => 3,
                  //'offset' => 3, - после какого поста выводить 
                  'order' => 'ASC'
                ];
                $query1 = new WP_Query($args);
               
                if($query1->have_posts()){
 
                  while($query1->have_posts()){
                    $query1->the_post(); ?>
                   <div class="grid_4">
                     <?php the_post_thumbnail(); ?>
                     <h3><?php the_title(); ?></h3>
                     <?php the_content(); ?>
                     <a href="<?php the_permalink(); ?>" class="btn">Read more</a>
                     <br><br>
                   </div>
                  <?php  }
                   wp_reset_postdata(); 
                }
 
                 else echo 'Записей нет.';
              ?>