我正在尝试修改我的分页代码,使其仅显示 Prev 和 Next 按钮,中间没有页面。事实证明这有点困难,因为 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'=> __('« Previous','cosmotheme'),
'next_text'=> __('Next »','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>';
}
} ?>