WordPress.org

Ready to get started?Download WordPress

Forums

Question about the loop (9 posts)

  1. chandamon
    Member
    Posted 7 years ago #

    Hello everyone,

    I am totally new and stupid to PHP,
    what I want is simple, I wanna make an archive page which can show 25 posts in it. That it shows 5 posts in each row needs to have a <br/> or </div> in the fifth post, tenth post and ect.

    Thus I need a code which can have a paragraph break when the post count is 5, so do you have any ideas? As I need to modify it for my project, I hope you can kindly help me. I really don't have any idea about it.

    Here is my page for reference (the content is just temporary):
    http://chandamon.com/YL/?cat=1

  2. Andrew Ozz
    WordPress Dev
    Posted 7 years ago #

    I think you can use the "get_posts()" template tag. get_posts('numberposts=5') for the 1st row,
    get_posts('numberposts=5&offset=5') for the 2nd
    get_posts('numberposts=5&offset=10') for the 3rd, etc.
    putting <div> or <br /> in between.
    Have a look here:
    http://codex.wordpress.org/Template_Tags/get_posts

  3. chandamon
    Member
    Posted 7 years ago #

    hey
    thanks a lot:D
    I try now

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Ugh! A better way would be to just keep a loop counter.

    // code to get 25 posts
    $counter=1;
    // start of the loop
    if ($counter!=5) $counter++;
    else
    {
    // do your output of a br or what have you
    $counter = 1; // reset counter
    }
    // end of your post loop
  5. chandamon
    Member
    Posted 7 years ago #

    thanks otto,
    I just have problem with the previous solution,

    it ends up like this:
    http://chandamon.com/YL/?cat=1

    now I try yours, thanks a lot.

  6. chandamon
    Member
    Posted 7 years ago #

    Hello Otto42, I know it is too rude and too much for me to ask so, but I really appreciate if you can help me with the codes I am using below by the method u provide...It is just perfect if you can show me exactly how I can I do it...

    I am really new to php, and I dont understand how the_post() works, so i dont really know how to implement the code you provided to this case...
    can you please do me favor? Since I am really in a hurry, sorry.

    Thanks for your kindly help .

    <?php while (have_posts()) : the_post(); ?>
    <div class="substory_frame">
    <div class="substory" id="left">
    <h3 class="substory_subhead"><?php the_time('j F Y'); ?></h3>
    <h3 class="substory_head">" rel="bookmark">
    <?php the_title(); ?>
    </h3>

    <?php the_excerpt(); ?>

    <h4 class="comments">" rel="bookmark">
    全文...
    »
    </h4>

    </div>
    <?php endwhile; ?>

    </div>

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Like so:

    <?php
    $counter = 1;
    while (have_posts()) : the_post();
    ?>
    <div class="substory_frame">
    ... all the other stuff here ...
    </div>
    <?php
    if ($counter != 5) $counter++;
    else {
    echo '';
    $counter=1;
    }
    endwhile;
    ?>
    ...

    Or something similar to that. You'll need to make the page show 25 posts at once though. You can do that by setting the post number to 25 in the options, but that will affect the whole site.

  8. chandamon
    Member
    Posted 7 years ago #

    Thanks a lot:D
    you are really nice:)

    yes
    currently I am stuck in the problem with the preset post number....

    Since I would like to have different numbers of posts in different archives and pages....

  9. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Add this before the "while" line then:
    query_posts($query_string . "&showposts=25");

    That will set it to 25 posts for that particular page only. You can put different ones on different types of pages to change the numbers for each page.

Topic Closed

This topic has been closed to new replies.

About this Topic