3

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

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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