I want while loop to show three posts on first section and the remaining in another section.i am very new to wordpress ,and not having much practical knowledge in php.any help would be very much appreciable.
here is my index.php code:
<!-- first section starting -->
<section class="main_section" id="masc">
<div class="container cardbox_1">
<div class="row cardbox_horizontal d-flex justify-content-center ">
<!-- while looping-section starting -->
<?php
$post_args = array( 'post_type' => 'Student Form', 'posts_per_page' => 6 );
$post_query = new WP_Query( $post_args );
if ( $post_query->have_posts() ) :
while ( $post_query->have_posts() ) : $post_query->the_post(); ?>
<?php if ( $post_query->the_posts() >= 3 ) : ?>
<!-- while loop -->
<div class=" col-md-4 cards">
<div class="card card_1">
<h3 class="h3 card_head text-uppercase">
<?php the_title(); ?>
</h3>
<div class="center_image d-flex justify-content-center">
<a rel="external" href="<php? the_permalink()?>">
<img class="image_head" src="<?php echo get_the_post_thumbnail_url(); ?>" alt="">
</a>
</div>
<div class="para_head"><?php the_content(); ?> </div>
<div class=" d-flex justify-content-end">
<a href="#" class="btn btn-primary">VIEW MORE</a>
</div>
<div>
</div>
<!-- while loop-->
<?php wp_reset_postdata(); ?>
<?php endwhile; // ending while loop ?>
<?php else: ?>
<p><?php _e( 'Sorry, no Students matched your criteria.' ); ?></p>
<?php endif; // ending condition ?>
<!-- while section ending -->
</div>
</div>
</div>
</section>
<!-- while section ending -->
<!-- middle section-(should not loop -this considered a border to top and bottom sections)ending -->
<section id="babg">
<div class="container">
<div class="row ">
<div class="col-md-12">
<hr class="this bbg">
</div>
</div>
</div>
</section>
<!-- middle section ending -->
<!-- here the same code again (this is faulty-i need the below post as numbered 4 )-->
<section class="main_section" id="masc">
<div class="container cardbox_1">
<div class="row cardbox_horizontal d-flex justify-content-center ">
<?php
$post_args = array( 'post_type' => 'Student Form', 'posts_per_page' => 3 );
$post_query = new WP_Query( $post_args );
if ( $post_query->have_posts() ) :
while ( $post_query->have_posts() ) : $post_query->the_post(); ?>
<div class=" col-md-4 cards">
<div class="card card_1">
<h3 class="h3 card_head text-uppercase">
<?php the_title(); ?>
</h3>
<div class="center_image d-flex justify-content-center">
<a rel="external" href="<php? the_permalink()?>">
<img class="image_head" src="<?php echo get_the_post_thumbnail_url(); ?>" alt="">
</a>
</div>
<div class="para_head"><?php the_content(); ?> </div>
<div class=" d-flex justify-content-end">
<a href="#" class="btn btn-primary">VIEW MORE</a>
</div>
</div>
</div>
<?php wp_reset_postdata(); ?>
<?php endwhile; // ending while loop ?>
<?php else: ?>
<p><?php _e( 'Sorry, no Students matched your criteria.' ); ?></p>
<?php endif; // ending condition ?>
</div>
</div>
</section>
<!-- pardon me for funny class names and my bad terminology -->
I want while loop to show three posts on first section and the remaining in another section.i am very new to wordpress ,and not having much practical knowledge in php.any help would be very much appreciable.
here is my index.php code:
<!-- first section starting -->
<section class="main_section" id="masc">
<div class="container cardbox_1">
<div class="row cardbox_horizontal d-flex justify-content-center ">
<!-- while looping-section starting -->
<?php
$post_args = array( 'post_type' => 'Student Form', 'posts_per_page' => 6 );
$post_query = new WP_Query( $post_args );
if ( $post_query->have_posts() ) :
while ( $post_query->have_posts() ) : $post_query->the_post(); ?>
<?php if ( $post_query->the_posts() >= 3 ) : ?>
<!-- while loop -->
<div class=" col-md-4 cards">
<div class="card card_1">
<h3 class="h3 card_head text-uppercase">
<?php the_title(); ?>
</h3>
<div class="center_image d-flex justify-content-center">
<a rel="external" href="<php? the_permalink()?>">
<img class="image_head" src="<?php echo get_the_post_thumbnail_url(); ?>" alt="">
</a>
</div>
<div class="para_head"><?php the_content(); ?> </div>
<div class=" d-flex justify-content-end">
<a href="#" class="btn btn-primary">VIEW MORE</a>
</div>
<div>
</div>
<!-- while loop-->
<?php wp_reset_postdata(); ?>
<?php endwhile; // ending while loop ?>
<?php else: ?>
<p><?php _e( 'Sorry, no Students matched your criteria.' ); ?></p>
<?php endif; // ending condition ?>
<!-- while section ending -->
</div>
</div>
</div>
</section>
<!-- while section ending -->
<!-- middle section-(should not loop -this considered a border to top and bottom sections)ending -->
<section id="babg">
<div class="container">
<div class="row ">
<div class="col-md-12">
<hr class="this bbg">
</div>
</div>
</div>
</section>
<!-- middle section ending -->
<!-- here the same code again (this is faulty-i need the below post as numbered 4 )-->
<section class="main_section" id="masc">
<div class="container cardbox_1">
<div class="row cardbox_horizontal d-flex justify-content-center ">
<?php
$post_args = array( 'post_type' => 'Student Form', 'posts_per_page' => 3 );
$post_query = new WP_Query( $post_args );
if ( $post_query->have_posts() ) :
while ( $post_query->have_posts() ) : $post_query->the_post(); ?>
<div class=" col-md-4 cards">
<div class="card card_1">
<h3 class="h3 card_head text-uppercase">
<?php the_title(); ?>
</h3>
<div class="center_image d-flex justify-content-center">
<a rel="external" href="<php? the_permalink()?>">
<img class="image_head" src="<?php echo get_the_post_thumbnail_url(); ?>" alt="">
</a>
</div>
<div class="para_head"><?php the_content(); ?> </div>
<div class=" d-flex justify-content-end">
<a href="#" class="btn btn-primary">VIEW MORE</a>
</div>
</div>
</div>
<?php wp_reset_postdata(); ?>
<?php endwhile; // ending while loop ?>
<?php else: ?>
<p><?php _e( 'Sorry, no Students matched your criteria.' ); ?></p>
<?php endif; // ending condition ?>
</div>
</div>
</section>
<!-- pardon me for funny class names and my bad terminology -->
Share
Improve this question
asked Jan 23, 2020 at 5:31
PranavmtnPranavmtn
13 bronze badges
1
- I am very sorry,there is a correction in the code.need to remove this: <?php if ( $post_query->the_posts() >= 3 ) : ?> after while begin ...exactly in 12th line – Pranavmtn Commented Jan 23, 2020 at 5:36
2 Answers
Reset to default 0What you are looking for is PHP's modulo operator and adjust markup as necessary:
<?php
$i = 0;
$did_hr = false;
$post_args = array( 'post_type' => 'Student Form', 'posts_per_page' => 6 );
$post_query = new WP_Query( $post_args );
if ( $post_query->have_posts() ) :
while ( $post_query->have_posts() ) : $post_query->the_post(); ?>
<?php if ( $i % 3 == 0 ) : ?>
<section class="main_section" id="masc">
<div class="container cardbox_1">
<div class="row cardbox_horizontal d-flex justify-content-center">
<?php endif; ?>
<!-- while loop -->
<div class=" col-md-4 cards">
<div class="card card_1">
<h3 class="h3 card_head text-uppercase">
<?php the_title(); ?>
</h3>
<div class="center_image d-flex justify-content-center">
<a rel="external" href="<?php the_permalink()?>">
<img class="image_head" src="<?php echo get_the_post_thumbnail_url(); ?>" alt="">
</a>
</div>
<div class="para_head">
<?php the_content(); ?>
</div>
<div class=" d-flex justify-content-end">
<a href="#" class="btn btn-primary">VIEW MORE</a>
</div>
</div>
</div>
<?php $i++; if ( $i != 0 && $i % 3 == 0 ) : ?>
</div>
</div>
</section>
<?php if ( ! $did_hr ) : $did_hr = true; ?>
<!-- middle section-(should not loop -this considered a border to top and bottom sections)ending -->
<section id="babg">
<div class="container">
<div class="row ">
<div class="col-md-12">
<hr class="this bbg">
</div>
</div>
</div>
</section>
<?php endif; ?>
<!-- middle section ending -->
<?php endif; ?><!-- while loop-->
<?php endwhile; // ending while loop ?>
<?php else: ?>
<p><?php _e( 'Sorry, no Students matched your criteria.' ); ?></p>
<?php endif; // ending condition ?>
If you want to show 3 posts in a list, you can follow this pattern:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DSC',
'posts_per_page' => 3
);
$post_query = new WP_Query($args);
if($post_query->have_posts()): while($post_query->have_posts()):$post_query->the_post();
?>
<div>
<h1> <?php the_title(); ?> </h1>
<?php the_excerpt(); ?>
</div>
<?php
endwhile;
endif;
?>
Now you want to show rest of the posts in another section but want to exclude the first 3 posts, use offset. Here is the example:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DSC',
'posts_per_page' => -1, // will show unlimited posts
'offset' => 3
);
$post_query = new WP_Query($args);
if($post_query->have_posts()): while($post_query->have_posts()):$post_query->the_post();
?>
<div>
<h1> <?php the_title(); ?> </h1>
<?php the_excerpt(); ?>
</div>
<?php
endwhile;
endif;
?>
You need to edit the code according to your need. Hope this will help you.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744809934a4594996.html
评论列表(0条)