WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Control the number of posts in The Loop (4 posts)

  1. livelearncreate
    Member
    Posted 7 years ago #

    Hello all. I just wanted to start a thread that matched up with what I was searching for all day, now that I've found a solution that works exactly how I want it. (without using a plugin)

    I've been working with using my WordPress blog for content on my sites. I've also built a site recently for a client that runs entirely off of WordPress as a Content Management System. It's fantastic.

    First things:

    If you are using the loop on a site and are essentially pulling your blog into this site for a news section or something... you can control how many posts feed into that page by putting an extra "if" statement inside the loop so that will break the loop once you've access the number of posts you want.

    [I've lost the link where I found this solution, if you have it feel free to credit them in a reply.]

    The easiest solution is if you only want one posting (the first posting). There you take the begining of your loop:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    And remove the "while" which as far as I understand is literally "the loop". So it will look like this:

    <?php if ( have_posts() ) : the_post(); ?>

    This will give you the first and only one post.
    ----------------------------------------------------------------------------------
    ----------------------------------------------------------------------------------
    Now if you are running a full loop, that is accessing your entire blog or even a specific category, this is a very easy solution to control exactly how many posts you want to see on that page.

    Here is the loop before the change (in this case I'm filtering only posts that are in category 2):
    ----------------------------------------------------------------------------------
    <?php if ( have_posts() ) : query_posts($query_string .'&cat=2'); while ( have_posts() ) : the_post(); ?>

    <div class="newsText">

    <b><?php the_time('M j, Y'); ?>   -   <?php the_title(); ?></b>

    <img src="newsline.gif" width="350px" height="1px" alt="" style="margin-top: 10px;" />

    <div class="entry">
    <?php the_content(); ?>
    </div>

    <p class="postmetadata"></p>
    </div>

    <?php endwhile; else: ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>

    ----------------------------------------------------------------------------------
    ----------------------------------------------------------------------------------
    Now here is the altered version that will only show the first 5 posts.
    ----------------------------------------------------------------------------------
    <?php if ( have_posts() ) : query_posts($query_string .'&cat=2'); while ( have_posts() ) : the_post(); ?>

    <?php static $ctr = 0;
    if ($ctr == "5") { break; }
    else { ?>

    <div class="newsText">

    <b><?php the_time('M j, Y'); ?>   -   <?php the_title(); ?></b>

    <img src="newsline.gif" width="350px" height="1px" alt="" style="margin-top: 10px;" />

    <div class="entry">
    <?php the_content(); ?>
    </div>

    <p class="postmetadata"></p>
    </div>

    <?php $ctr++; } ?>

    <?php endwhile; else: ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>
    ----------------------------------------------------------------------------------
    ----------------------------------------------------------------------------------
    In my case I only wanted five posts to show up but you can change the number in the php static section to control this.

    It works like a charm.
    I hope this helps any of you out there that have been trying to do the same thing without plug-ins.

  2. gspark
    Member
    Posted 7 years ago #

    how about displaying individual posts can we use include=1,2 in the_post ?

  3. whooami
    Member
    Posted 7 years ago #

    gspark, do you EVER reply to a thread that isnt over a month old??

  4. gspark
    Member
    Posted 7 years ago #

    haha well you see that's a problem this forum is made up of mostly very old posts or outdated tips (Doh!!)

    you realize how many ions i spend digging through these forums, wonder why i bother anyway, my eyeballs are crooked now :))

Topic Closed

This topic has been closed to new replies.

About this Topic