{WP} Limit posts displayed in the loop w/ code?

Discussion in 'Programming' started by Exnie, Jun 22, 2008.

  1. #1
    So I am coding a new design portfolio and using wordpress to do it. What I am having trouble with is on my main page I have four different loops running, all getting 'posts' from a different category (designs, news, replies, photos). I know that you can limit the amount of posts shown in the settings page but that limits the amount of posts displayed all together from all the loops. So I want there to be 2 design posts, 2 news posts, 4 replies posts and 2 photos posts but if I enter 10 total posts it displays them all from the design category.

    Does anyone know how to limit the amount of posts displayed DIRECTLY in the loop code?
     
    Exnie, Jun 22, 2008 IP
  2. Denn

    Denn Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I guess you are making a SQL call for every category or such.

    Just add a LIMIT in your sql query.
    SELECT * from x LIMIT 5 e.g.
     
    Denn, Jun 22, 2008 IP
  3. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #3
    In the loop of posts, increase a variable, when it's greater that the total number you want, break.

    Dan
     
    Danltn, Jun 22, 2008 IP
  4. BANAGO

    BANAGO Active Member

    Messages:
    456
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #4
    ('cat=1&showposts=2') Inclue this into the loop. The first part might not be correct.
     
    BANAGO, Jun 22, 2008 IP
  5. Exnie

    Exnie Active Member

    Messages:
    412
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #5
    I tryed what you said BANGO:

    <?php query_posts("showposts=3"); ?>
    Code (markup):
    but when I did that it showed one post repeated for hundreds of times down the page...
     
    Exnie, Jun 22, 2008 IP
  6. BANAGO

    BANAGO Active Member

    Messages:
    456
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #6
    I think you have put that at the wrong place. Where have put the category to be displayed? Please, bring a sample of your loop here, so I can tell you where to put it.
     
    BANAGO, Jun 23, 2008 IP
  7. Exnie

    Exnie Active Member

    Messages:
    412
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #7
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <?php query_posts("cat=18,-19,-20&showposts=3"); ?>
    <a href="#"><?php the_excerpt(''); ?></a>
    <?php the_title(); ?>
    <?php endwhile; ?>
    <?php endif; ?>
    Code (markup):
     
    Exnie, Jun 23, 2008 IP
  8. BANAGO

    BANAGO Active Member

    Messages:
    456
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #8
    You do not have to exclude 19 and 20. They are all automatically excluded. Try this and let me know.

     
    BANAGO, Jun 23, 2008 IP