Support » Themes and Templates » How to break a custom loop into multiple pages?

  • Iva

    (@supersonicsquirrel)


    Hello,

    I’m using the following code to display links to, well, all posts on a website.

    <?php $first = 0; //The first article to be displayed ?>
    
    <?php while(have_posts()) : the_post(); ?>
    <!--Post -->
    <div class="post">
    <h2 class="post-title"><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a><span class="arrow"></span></h2>
    <ul>
    <?php
    $myposts = get_posts('numberposts=-1&offset=$first');
    foreach($myposts as $post) :
    ?>
    <li><?php the_time('d/m/y') ?>: <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    <?php endwhile; ?>

    …and it’s a good idea on websites that don’t have 2000+ articles, which doesn’t apply to mine. I was wondering what I should add to this loop in order to break it into pages (say, 50-100 items per page) and, in general, if there’s a way to override the number of shown posts for other types of archives as well.

    Thank you. 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Try it with multiple loops [untested]:

    <?php
    $the_query = new WP_Query('posts_per_page=1');
    while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <div class="post">
    <h2 class="post-title"><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a><span class="arrow"></span></h2>
    <?php endwhile; ?>
    
    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('posts_per_page=50&offset=1&paged='.$paged); ?>
    <?php if ( have_posts() ) : ?>
    <ul>
    <?php while(have_posts()) : the_post(); ?>
    <!--Post -->
    <li><?php the_time('d/m/y') ?>: <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    <?php endif; ?>
    </div>

    Iva

    (@supersonicsquirrel)

    It works like a charm, thank you. 🙂

    Iva

    (@supersonicsquirrel)

    Actually, something’s wrong. It won’t show title of the page with the listing and the values all my pages pull from the db: last updated date and the edit link. Any idea…?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to break a custom loop into multiple pages?’ is closed to new replies.