0

i have following codes:

<div class="container">
<div class="row">
<?php
$slider_qu= new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'cat'   =>'',
'order' => 'title',
'orderby' => 'date',
'posts_per_page' =>'5',
));  
 while ( $slider_qu->have_posts() ) : $slider_qu->the_post(); $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), false, '' );
//Show the left hand side column
if($counter == 1) :
?>
    <div class="col-md-3 col-sm-3 col-xs-12 pad">

    <a href="<?php the_permalink() ?>" rel="bookmark" alt="<?php the_title_attribute(); ?>"> 
<div class="imgcontainer" style="background:<?php echo get_post_meta($post->ID, 'feature_bg', true); ?> url(<?php echo $src[0]; ?>);" >
<div class="Details"><h2><?php the_title_attribute(); ?></h2>
<span class="Date pull-right"><?php the_time( get_option('date_format') ); ?></span>
<span class="pull-left commentsNumerous" href="#"><i class="fa fa-comment"></i><?php comments_popup_link(__('0'), __('1'), __('%')); ?></span>
</div>
</div></a>

    </div>

<?php
//Show the middle column
elseif($counter == 2) :
?>
    <div class="col-md-6 col-sm-6 col-xs-12 pad">

    <a href="<?php the_permalink() ?>" rel="bookmark" alt="<?php the_title_attribute(); ?>"> 
<div class="imgcontainer" style="background:<?php echo get_post_meta($post->ID, 'feature_bg', true); ?> url(<?php echo $src[0]; ?>);" >
<div class="Details"><h2><?php the_title_attribute(); ?></h2>
<span class="Date pull-right"><?php the_time( get_option('date_format') ); ?></span>
<span class="pull-left commentsNumerous" href="#"><i class="fa fa-comment"></i><?php comments_popup_link(__('0'), __('1'), __('%')); ?></span>
</div>
</div></a>

    </div>
<?php
//Show the right hand side column
elseif($counter == 3) :
?>
    <div class="col-md-3 col-sm-3 col-xs-12 pad">

    <a href="<?php the_permalink() ?>" rel="bookmark" alt="<?php the_title_attribute(); ?>"> 
<div class="imgcontainer" style="background:<?php echo get_post_meta($post->ID, 'feature_bg', true); ?> url(<?php echo $src[0]; ?>);" >
<div class="Details"><h2><?php the_title_attribute(); ?></h2>
<span class="Date pull-right"><?php the_time( get_option('date_format') ); ?></span>
<span class="pull-left commentsNumerous" href="#"><i class="fa fa-comment"></i><?php comments_popup_link(__('0'), __('1'), __('%')); ?></span>
</div>
</div></a>

    </div>        

    <div class="clear"></div>
<?php endif; ?>

<?php $counter = ($counter == 3) ? 1 : ($counter + 1);
endwhile; ?>
</div></div>

which @Howdy_McGee write it here but its only show 1 post in every div but i want it to show 2 in first div and then 1 in second dive and another 2 posts in third div so i can make bootstrap grid slider like this.

  • Why don't you try to put your code inside a 1/3 class div only? – Fernando Baltazar Feb 6 '17 at 7:03
  • sorry sir but what do you mean? – Poorya Feb 6 '17 at 9:49
0

I haven't tested it but see if that works

Assign values to variables before begining of while loop

$c = 1;
$counter = 5;

Change your all $couter if conditions

if($counter == 1)
if($counter == 2)
if($counter == 3)

into this respectively

if($counter % $c==1 || $counter % $c==2)
if($counter % $c==3)
if($counter % $c==4 || $counter % $c==0)

At last before end of each $counter if conditions (in last line of each $counter if condtion) you need to increment $c like this

$c=$c+1;

NOTE Take backup of your code before trying.

UPDATE

The actual problem of your and mine previously posted answer was that, we were putting DIV inside while loop so it was creating again and again for every single post. Thats why 2 Posts in one DIV was not showing. But now I am giving you my tested solution which is working as per your requirements.

This solution require 3 while loop for all 3 divs and also different query of different while loop. And this time I am using Div outside while loop so it won't get create again and again. Although there might be a way to make it with only one while loop But I couldnt figure out, that's why I am using 3 loops in this solution.

Left DIV

<?php 
global $myOffset;
//Setting query for Left DIV
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
                'post_type' => 'post',  //Post type
                'posts_per_page' => 2,  //No. of posts
                'paged' => $paged       //For pagination(optional)
            );
$loop = new WP_Query( $args );
?>
<div class="left" style="width:30%;float:left;">
<?php
//While loop to show 2 posts in Left DIV
while ( $loop->have_posts() ) : $loop->the_post();
?>
    <h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><font style="color:#666666;"><?php the_title();?></a><span style="/* color: #d9d9d9; */font-size: 12px;">&nbsp;(<?php echo get_the_date('d.m.Y');?>)</span></h3>
<?php
endwhile;   //End of while loop of Left DIV
?></div> 

Middle DIV

<?php
//Setting query for Middle DIV
$myOffset = 2;
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
                'post_type' => 'post',  //Post type
                'posts_per_page' => 1,  //No. of posts to show
                'offset' => $myOffset,  //Excluding latest posts
                'paged' => $paged
            );
$loop = new WP_Query( $args );
?>
<div class="middle" style="width:30%;float:left;">
<?php
//While loop to show 1 post in Middle DIV
while ( $loop->have_posts() ) : $loop->the_post();
?>

    <h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><font style="color:#666666;"><?php the_title();?></a><span style="/* color: #d9d9d9; */font-size: 12px;">&nbsp;(<?php echo get_the_date('d.m.Y');?>)</span></h3>
<?php
endwhile;   //End of while loop of Middle DIV
?></div> 

Right DIV

<?php
//Setting query for Right DIV
$myOffset = 3;
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
                'post_type' => 'post',
                'posts_per_page' => 2,  //No. of posts
                'offset' => $myOffset,  //excluding latest posts
                'paged' => $paged
            );
$loop = new WP_Query( $args );
?>
<div class="right" style="width:30%;float:left;">
<?php
//While loop to show 2 posts in Right DIV
while ( $loop->have_posts() ) : $loop->the_post();
?>
    <h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><font style="color:#666666;"><?php the_title();?></a><span style="/* color: #d9d9d9; */font-size: 12px;">&nbsp;(<?php echo get_the_date('d.m.Y');?>)</span></h3>
<?php
endwhile;   //End of while loop of Right DIV
?></div> <?php

This is just an example (tested) in which I am only showing title of posts (nothing else). You need to put your div structure inside each while loop as per your need.

| improve this answer | |
  • Thanks for your anwser i try your way but its not work still every post in 1 div – Poorya Feb 6 '17 at 9:49
  • @Poorya Answer has been updated, See if that works for you and feel free to ask if you have any doubt regarding my updated answer. – Rishabh Feb 6 '17 at 14:42
  • thank you so much sir this works just fine, thanks for your time.and sorry because of my low reputation i cant upvote your anwser. – Poorya Feb 6 '17 at 16:04
  • @Poorya Ah don't worry about upvote, I am glad to help :) – Rishabh Feb 6 '17 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.