duanluanhui8348 2013-07-11 12:51
浏览 24
已采纳

我无法将以前的帖子链接显示出来

First of all, I've been all over Google, StackExchange and Codex but still can't solve my problem. It might be simple; am not sure. I have the following function that lists custom posts. The page has more than one query but only one (this one) make use of pagination. It's on the front page - which is set to static.

Here is the function:

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

The 'Next listings' links shows up linking only to page 2 no matter what page am on and the 'Previous Listings' link never show up.

What am I doing wrong?

WP: 3.5.2

  • 写回答

1条回答 默认 最新

  • dsuxcxqep31023992 2013-07-12 22:37
    关注

    I found a solution. I looked into the /wp-includes/link-template.php and found the two functions responsible for my mysery. I copied them to my theme and modified a little and everything works fantastically.

    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>';
    
    }
    

    Used those in place of previous_posts_link and 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); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?