0

I am creating a child theme archive template. I don't know how many posts will be displayed, which is fine except that i want to have a dividing line after each post except the last one.

I am using the usual loop, have added a $loopcount variable, and then an include to create the formatting/display and add the <div class="divider"></div>:

while ( have_posts() ) : the_post();
$loopCount++;
include(locate_template('template-parts/content-newsandevents.php'));
endwhile;

I have looked in the codex but I can't find a way to know how many posts the loop is going to do in advance. (I could use jQ to do this, but it would be neater to do it with php I think)

  • have you considered using get_template_part( 'template-parts/content', 'newsandevents' );? – Tom J Nowell May 24 '17 at 10:26
  • Yes - I can't use get_template_part because I need to send a variable into the include - hence the use of include(locate_template('template-parts/content-newsandevents.php')); – maxelcat May 24 '17 at 10:28
  • @TomJNowell Not everybody uses the get_template_part. Some people use the simplest themes in the old manner. – Max Yudin May 24 '17 at 11:13
  • have you tried using the :last css selector instead? – Tom J Nowell May 24 '17 at 12:41
  • yes, with a display:none - that would work. But i'd prefer not to create it ideally – maxelcat May 24 '17 at 12:42
2

Try reversing the logic, put the dividing line before the post but only do it after the first post.

To answer your question though, you can get the loop count with the following:

global $wp_query;
$count = count( $wp_query->posts );
// or
$count = $wp_query->post_count;
  • thanks for this Welcher. I tried both methods, and in the end went for the logic change - just neater that way. – maxelcat May 24 '17 at 12:49
0

By default it is 10.

You can change it from "Settings -> Reading" and then "Blog pages show at most"

URL - http://www.yoursite.com/wp-admin/options-reading.php

  • Yes thanks - I know that - but what if there are only 7 - there must be a way I can know how many the loop is going to do. – maxelcat May 24 '17 at 10: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.