Forums

How to break a custom loop into multiple pages? (4 posts)

  1. Iva
    Member
    Posted 6 months ago #

    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. :)

  2. keesiemeijer
    moderator
    Posted 6 months ago #

    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>
  3. Iva
    Member
    Posted 6 months ago #

    It works like a charm, thank you. :)

  4. Iva
    Member
    Posted 6 months ago #

    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...?

Reply

You must log in to post.

About this Topic