Post Limit, Category Filters
-
After going through the forums for the umpteenth time and with help from the irc channel (#wordpress at irc.freenode.net) finally, a solution to my long lasting problem.
With WP 1.2, you can now have multiple loops within one page. What is a WP loop? It’s everything between these two lines of code:
<!-- start WP motor --><?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
and
<!-- end WP motor --><?php endforeach; else: ?><?php _e('Sorry, no posts matched your criteria.'); ?><?php endif; ?>
Problem is what if you wanted to restrict certain loops to a certain limited number of posts and/or categories? Here’s the solution:
<!-- loop filter --><?php $counter = 0; if (in_category(2)
&&!$single
&&$counter < "3") { // sets counter to zero, sets filter category, sets counter limit ?>
Even if you have no prior experience with PHP, I’m sure you’ll be able to figure the code above out. The first part of the code sets the counter to zero. Within the “if” statement is the filter. What this example filter does is parse all posts under category 2, not in permalink form and loops twice.
The filter end is:
<!-- photolog filter end --><?php $counter++; } else { break; } // starts counter, then break if above conditions are met ?>
which starts the counter and breaks the loop once the conditions of the filter are met.
I know this is quite rudimentary… I am a noobie at PHP! Please help tidy the code up and adopt it as neccessary.
Edited: The ampersands are now fixed. Just copy and paste as neccessary.
- The topic ‘Post Limit, Category Filters’ is closed to new replies.