1

首先,我用过 Google、StackExchange 和 Codex,但仍然无法解决我的问题。这可能很简单;我不确定。我有以下列出自定义帖子的功能。该页面有多个查询,但只有一个(这一个)使用分页。它在首页 - 设置为静态。

这是功能:

function wight_listings()
{
    global $wp_query;
    global $page;

    $backup = $wp_query;
    $wp_query = NULL;
    $cur_page = $page; //get_query_var('page') ? get_query_var('page') : 1;

    $args = array(
            'post_type' => array('wight_listing'),
            'posts_per_page' => 7,
            'paged'=>$cur_page
        );
    $wp_query = new WP_Query($args);
    ?>
    <?php if ( $wp_query->have_posts() ) : ?>
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
        .
        .
        .
    <?php endwhile; ?>
        <div id="nav-posts" style="margin-top: .5em;">
            <div style="float:left;"><?php previous_posts_link('Previous Listings &laquo;'); ?></div>
            <div style="float:right;"><?php next_posts_link('&raquo; Next Listings'); ?></div>
            <div class="clear"></div>
        </div>
<?php  else: ?>
    <p>Oh no! There's nothing to show. :(</p>
<?php endif; ?>
<?php
    $wp_query = NULL;
    $wp_query = $backup;
}

无论在哪个页面上,“下一个列表”链接都只显示链接到第 2 页,并且“上一个列表”链接永远不会出现。

我究竟做错了什么?

可湿性粉剂:3.5.2

4

1 回答 1

1

我找到了解决方案。我查看了 /wp-includes/link-template.php 并找到了负责我的谜题的两个函数。我将它们复制到我的主题中并进行了一些修改,一切都非常好。

function wight_get_previous_posts_page_link($cur_page)
{
    if ( $cur_page > 1 )
    {
        $nextpage = intval($cur_page) - 1;
        if ( $nextpage < 1 )
            $nextpage = 1;
        return '<a href="' . get_pagenum_link($nextpage) . '">&laquo; Previous Listings</a>';
    }
}

function wight_get_next_posts_page_link($cur_page, $max_page)
{
    $paged = $cur_page;

    if ( !$paged )
        $paged = 1;
    $nextpage = intval($paged) + 1;

    if ( $max_page >= $nextpage )
        return '<a href="' . get_pagenum_link($nextpage) . '">Next Listings &raquo;</a>';

}

使用它们代替 previous_posts_link 和 next_posts_link。

<?php echo wight_get_previous_posts_page_link($cur_page); ?>
<?php echo wight_get_next_posts_page_link($cur_page, $query->max_num_pages); ?>
于 2013-07-12T22:37:15.620 回答