WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

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(get_option('inner_blog') && (get_query_var('cat') == get_option('blog_cat')))
        //call function to output posts as in a blog format
        //just a standard summary listing of posts in this category

//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>
    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;'));


    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>
share|improve this question
Are you looking for paged in WP_Query? codex.wordpress.org/Class_Reference/WP_Query – mor7ifer Apr 22 '12 at 21:52
Hmm, I'm not sure. Since I'm using get_posts(), would I need to use WP_Query instead? – N2Mystic Apr 23 '12 at 12:10
get_posts() is a wrapper for WP_Query. – mor7ifer Apr 23 '12 at 16:44
up vote 1 down vote accepted

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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.