0

I wanted to make last six posts different template from other posts but I can't get it work below are my loop code inserted on my index.php

$last_no_of_posts = 6;
$ctr = 0;

while ( have_posts() ) { the_post(); 

   if ($ctr < $wp_query->found_posts - $last_no_of_posts) {
      // require special template
      get_template_part( 'loop', 'special' );
   } else {
      // regular template
      get_template_part( 'loop', 'index' );
   }
   $ctr++;

}
  • Where are you trying to insert this content? In the index.php file, single.php, ...? – Pat J Jun 15 '13 at 20:44
  • I insert this loop on index.php – idontknowhow Jun 15 '13 at 21:00
  • This seems like it should work. What exactly happens? Try echoing variables in the loop and see if they are what you expect from iteration to iteration. – Rarst Jun 15 '13 at 21:48
  • Do you have files named loop-special.php and loop-index.php in your theme directory? – Pat J Jun 15 '13 at 22:29
  • @Rarst it doesnt show anything even to post that should use get_template_part( 'loop', 'index' ); – idontknowhow Jun 16 '13 at 7:18
1

As written, you are checking the total post count against your counter. That is, $wp_query is the total post count for the query not the per page count.

Now, assume 30 posts total in the query with 10 posts per page. Your counter starts and will get all the way to 10, but $ctr < $wp_query->found_posts - $last_no_of_posts is always true. $ctr will be 10 at the end of that loop but $wp_query->found_posts - $last_no_of_posts will be 30 - 10, or 20. The same with the second page of results, because your counter starts over. The same with the page after that. Under that circumstance, and many like it, $ctr is always less than found_posts minus your counter. The only way this identifies the last six is if all results are on one page-- no pagination. Perhaps that is what you are doing, and that is what you want.

However, if you need to identify the last six on each page of pagination you need to rewrite it to use post_count not found_posts:

while ( have_posts() ) { the_post(); 

   if ($ctr < $wp_query->post_count - $last_no_of_posts) {
      // require special template
      get_template_part( 'loop', 'special' );
   } else {
      // regular template
      get_template_part( 'loop', 'index' );
   }
   $ctr++;

}

If you want the last six on the very last page of pagination you need (untested):

if ($wp_query->current_post >= $wp_query->found_posts - $last_no_of_posts) {
  • I need it to identify the last six post of total post not per page :) – idontknowhow Jun 16 '13 at 7:16
  • As I explained, what you are doing is still not going to work except in some restricted circumstances because your counter won't continue incrementing over each page but will reset on each page. You need to be checking the current_post value as per the last sentence in the answer. – s_ha_dum Jun 16 '13 at 13:27

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.