Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

DanUnion on "Query number of posts in search result and load content accordingly"

$
0
0

Hi there,

wondering if someone could help.

On my search results page i currently have this code, which loads a content template for the posts when it finds posts, or if no posts are found, loads a different template.

<?php if ( have_posts() ) : ?>
		<?php /* Start the Loop */ ?>
		<?php while ( have_posts() ) : the_post(); ?>
			<?php get_template_part( 'content', 'grid' ); ?>
		<?php endwhile; ?>

		<?php else : ?>
			<?php get_template_part( 'content', 'none' ); ?>
<?php endif; // end have_posts() check ?>

What im wanting to do though, is display content templates depending on how many posts are returned in a search. So if there are 7 or less posts returned, it uses one template for each post displayed, and if there are more than 7 posts, it uses a different template.

Ive taken a look at using:

$posts = get_posts();
$count = count($posts);

and when i echo $count, it seems to show the number of returned posts, however for the life of me i cant seem to build a query on the returned number of posts into the loop, any help would be much appreciated!!


Viewing all articles
Browse latest Browse all 8245

Trending Articles