0

I need to create a sliders contain 3 post title for each slide. I can create it by one title for each slide but not 3 posts Could you please help me This is My code

  <ul class="overview">
       <?php 
            $newspost = new WP_Query();
             newspost->query('cat=10&showposts=6&post_type=page'); 
          while ($newspost->have_posts()) : $newspost->the_post(); 
          ?>
               <li>
                  <a href="<?php the_permalink();?>">
                   <p class="testimonial_name"><?php the_title(); ?></p>
                  </a>   
               </li>
  <?php    endwhile;?>
    </ul>
  • Please show us what have you tried so far. – Nilambar Jun 18 '15 at 9:50
  • <ul class="overview"><?php $newspost = new WP_Query(); newspost->query('cat=10&showposts=6&post_type=page'); while ($newspost->have_posts()) : $newspost->the_post(); ?> <li> <a href="<?php the_permalink();?>"> <p class="testimonial_name"><?php the_title(); ?></p> </a> </li> <?php endwhile;?></ul> – Sirina Jun 18 '15 at 10:08
  • Please edit your question and update with your code. Do not post in comment. – Nilambar Jun 18 '15 at 11:16
1

There are two popular solutions for this. You can:

Alternative 1:

Add a counter to the while, and do something special if the counter reaches 3:

$i = 0;
while(...) {
  $i++;
  if ($i == 3) {
    ...
    $i = 0;
  }
}

This is difficult to do here because you will probably end up with an empty or unclosed li.

Alternative 2:

Use get_posts instead of WP_Query (probably what you should use anyway). Use array_chunk to split the array of posts into chunks.

  • 1
    get_posts() uses WP_Query, so it actually doesn't really matter. The only advantage is that get_posts() is a bit faster due to the use of 'no_found_rows' => true :-) – Pieter Goosen Jun 18 '15 at 12:44
  • How did you test it? – Brad Dalton Jun 18 '15 at 13:01
  • 1
    @BradDalton I'm not sure if this is directed at me, so I will answer according to my comment. Add a timer before and after the query and subtract the two times from each other. You'll see that get_posts is faster as pagination is legally broken and skipped by get_posts. This is all done by 'no_found_rows' => true. Just a tip, if you need to address someone specific, use the @ sign and the person's name, otherwise only the answerer and question asker gets the message, not the other participants :-) – Pieter Goosen Jun 18 '15 at 13:23
  • Sorry yes @goose, forgot your name! haha. Thanks for the useful answer – Brad Dalton Jun 18 '15 at 13:47

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.