jbeltran
Member
Posted 2 years ago #
Hi,
I modified my search page so that the search results have a line between each entry. What I did was that I included a <hr> tag inside the loop. The problem with this is that it adds a line even after the last result.
I guess the solution is to have an if statement determining whether the next result will be the last one. Unfortunately, I'm having problem finding out how to do this. Any thoughts/alternative solutions would be appreciated!
Thanks
This post showed me how to do this: Determine the last post in a WordPress loop
I did the following on one of my sites to loop through a specific category and have a "last" class on the final post:
<ul>
<?php $query = new WP_Query('category_name=products&orderby=title&order=asc'); ?>
<?php if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<li <?php if(($query->current_post + 1) == ($query->post_count)) {echo 'class="last"';};?>><h3><?php the_title();?></h3></li>
<?php endwhile; else: ?>
<li>Sorry, no products found.</li>
<?php endif; ?>
</ul>
Munksey
Member
Posted 2 years ago #
There's an easier way of doing it, just run has_posts() again.