$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Show post content and title in diferent divs using WP_Query using a loop|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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; ?>
Post a comment

comment list (0)

  1. No comments so far