在自定义查询上生成分页链接时遇到问题

时间:2012-12-28 作者:John B

我在初始循环后的页面上使用自定义查询来列出与该页面关联的类别中的一些帖子。我正在这样做:

    query_posts(array(
       \'category_name\' => $name,
       \'orderby\' => \'date\',
       \'order\' => \'DESC\',
       \'posts_per_page\' => 1,
       \'paged\' => $paged));
我也试过使用get_query_var(\'paged\') 代替$paged btw。

然后我做循环,它显示了正确的帖子数量,1。如果我使用previous_posts_link()next_posts_link(), 但是,如果我使用echo paginate_links() 在我的循环之后,甚至在我的循环中,都不会生成任何链接,因为我目前在该类别中有2篇文章,所以我希望看到至少一个指向第2页的链接。似乎有很多关于传递正确信息以查询此实例的帖子的信息,但没有使用什么函数来实际呈现分页链接,因此我非常感谢您的帮助。谢谢

编辑:(完整代码)

<div id="left_column_wrap">
<?php 
    if (have_posts()) : while (have_posts()) : the_post(); ?>

    <article class="post" id="post-<?php the_ID(); ?>">

        <h1><?php the_title(); ?></h1>
        <?php //include (TEMPLATEPATH . \'/_/inc/meta.php\' ); ?>
        <div class="entry">
            <?php the_content(); ?>
            <?php //wp_link_pages(array(\'before\' => \'Pages: \', \'next_or_number\' => \'number\')); ?>
        </div>
        <?php edit_post_link(\'Edit this entry.\', \'<p>\', \'</p>\'); ?>

    </article>
    <?php endwhile; endif;

    $name = \'\';
    if(isset($pagename))
    {
        $name = $pagename;
    }
    if($name == \'5a\' || $name == \'4a\' || $name == \'3a\' || $name == \'2a\')
    {
        echo \'<div id="all_cats_recent_posts_wrap">\';
        query_posts(array(\'category_name\' => $name, \'orderby\' => \'date\', \'order\' => \'DESC\', \'posts_per_page\' => 1, \'paged\' => $paged));

        if (have_posts()):
            while (have_posts()):
                the_post(); ?>          
                <article <?php post_class() ?> id="post-<?php the_ID(); ?>">

                <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                <?php include (TEMPLATEPATH . \'/_/inc/meta.php\' ); ?>
                <div class="entry">
                    <?php echo substr(strip_tags(get_the_content()), 0, 280).\'...\';?>
                    <a class="quick_link" href="<? the_permalink()?>">(read more...)</a>                    
                </div>
            </article>
            <?
            endwhile;
            echo paginate_links();
            ?>
            <div class="alignleft"><?php previous_posts_link(\'&laquo; Previous\') ?></div>
            <div class="alignright"><?php next_posts_link(\'More &raquo;\') ?></div>
            <?
        endif;
        echo \'</div>\';

    }
    ?>
</div>

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

虽然所有参数都是可选的,paginate_links 如果没有争论,不一定做任何事情。看看法典中的例子。

global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
    \'format\' => \'?paged=%#%\',
    \'current\' => max( 1, get_query_var(\'paged\') ),
    \'total\' => $wp_query->max_num_pages
) );
这很有效。试试看。现在删除最后一个参数。看见试着传递那个total 您的功能。

同样,请不要使用query_posts, 尤其是二次回路。使用get_posts 或创建新的WP_Query 对象我会做后者。我认为你真的不需要改变太多。

$myquery = new WP_Query(
    array(
        \'category_name\' => $name, 
        \'orderby\' => \'date\', 
        \'order\' => \'DESC\', 
        \'posts_per_page\' => 1, 
        \'paged\' => $paged
    )
);

结束

相关推荐

Pagination on custom query

我正在使用以下代码query_posts 要组合自定义搜索,请执行以下操作:$args = array( \'post_type\' => \'species\', \'meta_query\' => $meta_query, \'tax_query\' => $tax_query ); $meta_query 由以下几段代码组成:if (!empty($_POST[\"s_aquarium_H\"])) {