How to add paging to query

时间:2012-04-22 作者:N2Mystic

在下面的脚本中,我从存档中调用。php到函数。php检索已标记在为我的“博客”帖子保留的类别中的帖子列表。我在总结中列出了这些帖子,其数量由一个名为“blog\\u count”的选项决定。

我需要将分页添加到此函数中,以便blog\\u计数确定初始页面计数,而不是返回的日志总数。要包含此分页,需要对get\\u posts()调用进行哪些更改?

例如,我尝试添加“offset”=>3,但它只列出了offset帖子,而不是之前的帖子。

//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
 }

1 个回复
最合适的回答,由SO网友:getWeberForStackExchange 整理而成

而不是使用next_posts_linkprevious_posts_link, 尝试paginate_links. 它允许您指定当前页和总页数。

例如:

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

结束

相关推荐