尝试仅使用“上一页”和“下一页”按钮来实现 WP 分页

问题描述 投票:0回答:2

我正在尝试修改分页代码,使其仅显示“上一页”和“下一页”按钮,中间不显示页面。事实证明,这有点困难,因为 paginate_links 函数在参数方面没有太多选项,我可以传递这些选项来禁用页面的显示。

还使用 next_posts_link() 和 previous_posts_link() 函数并没有真正的帮助,因为它链接到 mydomain.com/page/2 而我的默认主页类似于 mydomain.com/?fp_type=hot (显示特定类型的自定义帖子)

这是我的分页代码

<?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>';
    }
} ?>
php wordpress pagination
2个回答
0
投票

将此代码添加到您要显示帖子的页面。

 <?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);?>

在邮政编码后添加以下代码:

         <?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>

0
投票

您可以使用 posts_nav_link() 函数。

© www.soinside.com 2019 - 2024. All rights reserved.