How to add paging to query

In the script below, I’m calling out from archive.php to functions.php to retrieve a list of posts that have been flagged in a category reserved for my “blog” posts. I’m listing these posts in summary with the count determined by an option called “blog_count”.

I need to add paging to this function so that the blog_count determines the initial page count, not the total number of blog posts returned. What changes do I need to make to the get_posts() call to include this paging?

Read More

I’ve tried adding ‘offset’ => 3 for example, but it only list the offset posts, not the previous.

//called from archive.php to handle posts placed into "blog" category
if(is_category())
{
    if(get_option('inner_blog') && (get_query_var('cat') == get_option('blog_cat')))
    {
        //call function to output posts as in a blog format
        get_blog_links();
    } 
    else 
    { 
        //just a standard summary listing of posts in this category
        get_category_links();
    }
}?> 


//function inside functions.php 
function get_blog_links(){
    $hasChildCats = get_term(get_query_var( 'cat' ), 'category');
    if($hasChildCats->count <= 0) return;
    global $post;
    $cat = get_query_var( 'cat' );
    //set the target category and initial page post count
    $args = array('cat' => "$cat",'post__not_in' => get_option('sticky_posts'),'numberposts' => get_option('blog_count'));
    $myposts = get_posts($args);echo '<div class="blog"></div>'; 

    //loop over the resultset
    foreach($myposts as $idx=>$post){ ?>
    <article><h3 class="blogTitle"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    <?php 
    setup_postdata($post);
    echo '<address>Posted on: <time datetime="'.get_the_time('Y-m-d').'">'.get_the_date().'</time> By: '.get_the_author().'</address>';
    if(has_post_thumbnail() && get_option('cb2_show_thumbs')) the_post_thumbnail('thumbnail', array('class' => 'alignright', 'style' => 'margin:10px;'));

    get_blog_excerpt();

    if(has_post_thumbnail() && get_option('show_thumbs')) echo '<div style="clear:both">&nbsp;</div>';?></article>
    <?php } echo '</div></div>';
    ?>
    <div class="navigation">
        <span class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></span>
        <span class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></span>
    </div>
    <?php
 }

Related posts

Leave a Reply

1 comment

  1. Rather than using next_posts_link and previous_posts_link, try paginate_links. It lets you specify the current page and total page count.

    For example:

    echo paginate_links( array(
        'base' => add_query_arg( 'cpage', '%#%' ),
        'format' => '',
        'prev_text' => __('&laquo;'),
        'next_text' => __('&raquo;'),
        'total' => ceil($total / $items_per_page),
        'current' => $page
    ));