搜索查询的分页格式不正确WP

时间:2018-10-23 05:54:12

标签: wordpress search pagination

我在下面的分页代码中似乎似乎想出了如何从分页的第一个链接中省略/ page / 1 /,例如'base'=> get_pagenum_link(1)。 '%_%',例如新闻文章和档案页等。但是现在在搜索查询http://url.com/?s=query上,下一个分页以?s = querypage2而不是/ page / 2 /?s = query结束,结果是这样以前是

如何在搜索查询中获取它,它在查询之前将page / 2 /移动了?谢谢

function vb_pagination( $query=null ) {
  global $wp_query;
  $query = $query ? $query : $wp_query;
  $big = 999999999;
   $paginate = paginate_links( array(
    //'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ),
    'base' => get_pagenum_link(1) . '%_%',
    'type' => 'array',
    'total' => $query->max_num_pages,
    'current' => max( 1, get_query_var('paged') ),
    'prev_text' => __('«'),
    'next_text' => __('»'),   
     )
  );
  if ($query->max_num_pages > 1) :
  ?>
  <nav>
  <ul class="pagination">
  <?php
  foreach ( $paginate as $page ){
      echo '<li>' . $page . '</li>';
   }
  ?>
  </ul>
  </nav>
  <?php
     endif;
}

1 个答案:

答案 0 :(得分:0)

似乎通过添加条件base

得出了答案
function vb_pagination( $query=null ) {
  global $wp_query;
  $query = $query ? $query : $wp_query;
  $big = 999999999;
  $searchQ = str_replace( home_url(), '', '%_%' . get_pagenum_link(1));  
  $postsPagesBase = get_pagenum_link(1) . '%_%';
  if(!is_search()){
  $paginate = paginate_links( array(
  'base' => $postsPagesBase,
  'type' => 'array',
  'total' => $query->max_num_pages,
  'current' => max( 1, get_query_var('paged') ),
  'prev_text' => __('&laquo;'),
  'next_text' => __('&raquo;'),   
   )
 );
} else {
$paginate = paginate_links( array(
  'base' => $searchQ,
  'format' => '/page/%#%',
  'type' => 'array',
  'total' => $query->max_num_pages,
  'current' => max( 1, get_query_var('paged') ),
  'prev_text' => __('&laquo;'),
  'next_text' => __('&raquo;'),   
  )
);
}
if ($query->max_num_pages > 1) :
  ?>
  <nav>
  <ul class="pagination">
  <?php
  foreach ( $paginate as $page ){
      echo '<li>' . $page . '</li>';
   }
  ?>
  </ul>
  </nav>
  <?php
     endif;
}