duanping2809 2013-07-20 18:58
浏览 96

尝试仅使用Prev和Next按钮实现WP分页

I'm trying to modify my code for pagination in such a way that it only displays the Prev and Next buttons with no pages in the middle. This is proving to be kind of hard because paginate_links function doesn't have many options in terms of arguments which I can pass to disable the pages from being displayed.

Also using next_posts_link() and previous_posts_link() functions doesn't really help because that links to mydomain.com/page/2 whereas my default homepage is something like this mydomain.com/?fp_type=hot (to display particular kind of custom posts)

This is my pagination code

<?php
global $wp_query;
global $wp_rewrite;

if( is_search() ){
    if( (int) get_query_var('page') > 0 ){
        $current = get_query_var('page');
    }else{
        if( (int) get_query_var('paged') > 0 ){
            $current = get_query_var('paged');
        }else{
            $current = 1;
        }
    }
    $wp_query = new WP_Query('paged='. $current . '&s='.get_query_var('s') );

    $pagination = array(
        'base' => @add_query_arg( 'paged' , '%#%' ),
        'format' => '',
        'total' => $wp_query -> max_num_pages,
        'current' => $current,
        'show_all' => false,
        'prev_next'=> true,
        'prev_text'=> __('&laquo; Previous','cosmotheme'),
        'next_text'=> __('Next &raquo;','cosmotheme'),
        'type' => 'array'
    );

    if( $wp_rewrite->using_permalinks() ){
            $pagination['base'] = user_trailingslashit( trailingslashit(  remove_query_arg( 'search', remove_query_arg( 's', get_pagenum_link( 1 ) ) ) ) . 'page/%#%/', 'paged' );
    }

    if( !empty($wp_query->query_vars['s'] ) ){
            $pagination['add_args'] = array( 's' => urlencode( get_query_var( 's' ) ) );
    }

    $pgn = paginate_links( $pagination );

    if( !empty( $pgn ) ){
        echo '<div class="pag">';
        echo '<ul class="b_pag center p_b">';
        if( $current == 1 ){
            $current--;
        }
        foreach($pgn as $k => $link){
            print '<li>' . str_replace("'",'"',$link) . '</li>';

        }
        echo '</ul>';
        echo '</div>';
    }
}else{
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;

    $pagination = array(
            'base' => @add_query_arg('paged','%#%'),
            'format' => '',
            'total' => $wp_query->max_num_pages,
            'current' => $current,
            'show_all' => false,
            'type' => 'array'
            );

    if( $wp_rewrite->using_permalinks() ){
            $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'fp_type' , remove_query_arg( 'type' , remove_query_arg( 's', get_pagenum_link( 1 ) ) ) ) ) . 'page/%#%/', 'paged' );
    }

    if( !empty($wp_query->query_vars['s'] ) ){
            $pagination['add_args'] = array( 's' => urlencode( get_query_var( 's' ) ) );
    }

    if( !empty( $wp_query->query_vars['type'] ) ){
            $pagination['add_args'] = array( 'type' => get_query_var( 'type' ) );
    }

    if( !empty( $wp_query->query_vars['fp_type'] ) ){
            $pagination['add_args'] = array( 'fp_type' => get_query_var( 'fp_type' ) );
    }

    $pgn = paginate_links( $pagination );
    if( $current == 1 ){
        $current--;
    }

    if(!empty($pgn)){
        echo '<div class="pag">';
        echo '<ul class="b_pag center p_b">';
        foreach($pgn as $k => $link){
            print '<li>' . str_replace( "'" , '"' , $link ) . '</li>';
        }
        echo '</ul>';
        echo '</div>';
    }
} ?>
  • 写回答

1条回答 默认 最新

  • duanji7182 2013-07-23 06:57
    关注

    Add this code to that page where you want to show the post.

     <?php
    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
       'cat' => '7',
       'post_type' => 'post',
       'posts_per_page' => 10,
       'paged' => $page,
       );
    
    query_posts($args);?>
    

    add below code after the post code:

             <?php while ( have_posts() ) : the_post(); ?>
             <div> 
             <ul>
             <li>
              <?php the_content(); ?>
              </li>
             </ul>
             </div>
            <?php endwhile; // end of the loop. ?>
           <div class="navigation"><p><?php posts_nav_link(); ?></p></div>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题