wp query - Show post content and title in diferent divs using WP_Query using a loop

admin2025-06-06  1

im trying to display the post title and post content of posts in diferent divs using a loop to iterate the posts in $the_query, how can achieve this?

<?php $the_query = new WP_Query( 'posts_per_page=4' );?>

the structure should be something like this

<div class="grid">
   <div class="main post"> 
      <!--show data of the first post in the array-->
   </div>
   <div class="nested">
      <div class="first post">
         <!--show data of the second post in the array-->
      </div>
      <div class="second post">
         <!--show data of the third post in the array-->
      </div>
      <div class="third post">
         <!--show data of the fourth post in the array-->
      </div>
   </div> <!--end nested-->
</div> <!--end grid-->

im trying to display the post title and post content of posts in diferent divs using a loop to iterate the posts in $the_query, how can achieve this?

<?php $the_query = new WP_Query( 'posts_per_page=4' );?>

the structure should be something like this

<div class="grid">
   <div class="main post"> 
      <!--show data of the first post in the array-->
   </div>
   <div class="nested">
      <div class="first post">
         <!--show data of the second post in the array-->
      </div>
      <div class="second post">
         <!--show data of the third post in the array-->
      </div>
      <div class="third post">
         <!--show data of the fourth post in the array-->
      </div>
   </div> <!--end nested-->
</div> <!--end grid-->
Share Improve this question asked Nov 26, 2018 at 19:04 TraukoTrauko 251 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

PHP loops are loops - you can control them ;)

<?php
    $the_query = new WP_Query( array('posts_per_page' => 4) );
    if ( $the_query->have_posts() ) :
        $the_query->the_post();
?>
<div class="grid">
   <div class="main post">
      <!--show data of the first post in the array-->
      <?php the_title(); ?>
      <?php the_content(); ?>
   </div>
   <div class="nested">
      <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
      <div class="post">
         <!--show data of next post in the array-->
         <?php the_title(); ?>
         <?php the_content(); ?>
      </div>
      <?php endwhile; ?>
   </div> <!--end nested-->
</div> <!--end grid-->
<?php endif; ?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749150713a316797.html

最新回复(0)