Support » Plugins and Hacks » 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.

Viewing 15 replies - 1 through 15 (of 25 total)
  • Not sure what’s wrong with the encoding… but the amp;amp bits in the code above shd be substituted with the appropiate ampersand symbol.

    Yeap, fixed it! Thanks Beel.

    This isn’t working for me. What version WP is this for?

    Its for 1.2 but somehow even I can’t get it to work! I’m not too sure why though. Still fixing it…

    ok… i’m rather lost.. *’.’* _Where_ would I put these codes? When I place them in the index.php, I get an error. Thank you!

    Thanks for spelling this out, very helpful! My home page uses three seperate cateogories, so I have three of these loops in a row. One change I had to make is to reset the counter at the endof the main foreach loop:
    <?php endforeach; endif; $counter = 1; ?>
    otherwise, it remains set at whereever it arrived at the end of the previous go-round…

    Okay… I’m still rather confused.. *blush* How would I apply this, in relation to this situation? http://wordpress.org/support/10/6012/

    Great stuff Zaaba, this is going to be useful.
    Andrew

    Hmm.. I am continuously getting an error msg, citing a parse error. Help! I followed the intructions given above..

    Hmm.. any ideas as to how you implemented this? Perhaps someone who has managed to successfully do this can post their entire loop (did I get it right?) here, so that others (mainly me! =op) can learn? Thank you!

    I am using this on my homepage and it loops both categories fine, but then when I click on a link to go to the article, everything goes blank. And when I click on links in my menu to go to all posts in a certain category, the first loop goes blank. Any ideas on how to solve this problem?

    I’m getting the same problem. My site is Filmnite.com.
    I tried putting the loop code in a separate include file, but I still get the same problem – it works on the main page, but not on the individual pages. I’m using m0d_rewrite to get clean URIs.
    I don’t know much about PHP, but my guess is that something is different about the $post and $posts variables so that they have values from elsewhere in the page that interfere with the archive links being displayed properly. Anyone know how to reset them? Thanks!
    Here’s the code in my catlinks.php include:

    <!-- Generate links to recent posts/pages
    Instructions:
    1. Change the title below to whatever you want.
    2. Change the # in in_category(#) to whatever category you want to list posts for.
    3. Change the 3 in $counter > "3" to the number of posts you want to display links to.
    4. Copy and paste this loop for each category you want to display, and change the above values as necessary. Note: This does require some server processing overhead, so use it sparingly.
    For further documentation/author info see this thread:
    http://wordpress.org/support/index.php?action=vthread&forum=10&topic=6012&page=0
    -->

  • Upcoming
    • <!-- category-based link loop -->
      <?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
      <?php static $counter = 1; if ( $counter > "3" ) { break; } else { if ( in_category(2) && !$single ) { ?>

    • "><?php the_title(); ?>
    • <?php $counter++; } } ?>
      <?php endforeach; endif; $counter = 1; ?>
      <!-- End of category-based link loop -->

  • Previous
    • <!-- category-based link loop -->
      <?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
      <?php static $counter = 1; if ( $counter > "3" ) { break; } else { if ( in_category(3) && !$single ) { ?>

    • "><?php the_title(); ?>
    • <?php $counter++; } } ?>
      <?php endforeach; endif; $counter = 1; ?>
      <!-- End of category-based link loop -->

Anonymous & Justin: From what I know, and correct me if I’m wrong, the reason why links result in blank pages is because:
1. The loop has not been reset to ONE. If that is the case, loops will break because the loop threshold has been met. Remember to put the revised WP loop end (see above post). I see you have already done this Justin so ignore this point 🙂
2. Another reason is the !$single rule has been invoked. You might want to try toggling this on/off by deleting/adding the ! symbol. Remember that the ! results in the NOT logical operator. If you have the the !$single rule in your filter, the loop will NOT SHOW if you are in “single-post” mode (e.g. what happens when you click on a permalink). If you want the loop to show regardless of whether you are in full index mode or single post (permalink) mode then just delete the && !$single rule. Ommitting the ! altogether results in the loop ONLY SHOWING if in single-post mode.
For example, my site at http://www.truedeath.com has 2 loops: the central blog loop and the photoblog loop. The central blog loop calls all posts BUT from the photoblog category. The photoblog loop at the side calls posts from ONLY the photoblog category and ONLY if NOT in single-post mode ie: only if we’re on the main index page else it will be hidden.
Ah, but this posed a problem: what if the visitor clicks on a permalink of a photo? The index file would not display it because the category filter would filter it out. My solution then was to create another page – photoblog.php which is the exact clone of index.php but with the photoblog category filter reversed so that it would ONLY show posts from the photoblog category. This was the fastest workaround I could come to without uber hacking the index file any further than I had already done above.
The operating logic above is really quite simple. I guess if you want to have more complex rules you’ll have to tinker alot more with the if statements. If its only 1 index file that you want to worry about then perhaps it would be best to come up with many loops (as in MANY loops) with each having its own set of rules. You can set some to only appearing in single-post mode and some in all modes but excluding certain categories. This is probably very cumbersome but I’m sure with much PHP engineering and dilligence you could combine many of those rules and if statements into one single block.
Might I suggest writing out on a sheet of paper what exactly you want visible at any time and then cater those rules to fit around those permutations. Remember to take note of the ! NOT operator! Good luck!

Hurrah! I’ve got it working! Thank you!

Viewing 15 replies - 1 through 15 (of 25 total)
  • You must be logged in to reply to this topic.