最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

paginate links - paginate_links and query vars

programmeradmin2浏览0评论

I have using the Wordpress paginate_links() to build a paginator on a custom archive template for a custom post type. My site uses permalinks:

<?php echo paginate_links(array(
    'base' => get_pagenum_link(1) . '%_%',
    'current' => max(1, get_query_var('paged')),
    'format' => 'page/%#%',
    'total' => $wp_query->max_num_pages,
)); ?>

This works fine until I try to add query string vars to build a custom search string when the paginator echoes:

.../entries?post_type=entry&s=testpage/1
.../entries?post_type=entry&s=testpage/2

Instead of:

.../entries/page/1?post_type=entry&s=test
.../entries/page/2?post_type=entry&s=test

and so on... How can I get the correctly formatted URLs?

I have using the Wordpress paginate_links() to build a paginator on a custom archive template for a custom post type. My site uses permalinks:

<?php echo paginate_links(array(
    'base' => get_pagenum_link(1) . '%_%',
    'current' => max(1, get_query_var('paged')),
    'format' => 'page/%#%',
    'total' => $wp_query->max_num_pages,
)); ?>

This works fine until I try to add query string vars to build a custom search string when the paginator echoes:

.../entries?post_type=entry&s=testpage/1
.../entries?post_type=entry&s=testpage/2

Instead of:

.../entries/page/1?post_type=entry&s=test
.../entries/page/2?post_type=entry&s=test

and so on... How can I get the correctly formatted URLs?

Share Improve this question edited Oct 21, 2013 at 19:55 benedict_w asked Oct 21, 2013 at 19:43 benedict_wbenedict_w 5912 gold badges7 silver badges17 bronze badges 1
  • What exactly is adding those vars? – Rarst Commented Oct 21, 2013 at 21:05
Add a comment  | 

2 Answers 2

Reset to default 9

Seems that the query string is coming from the base argument call to get_pagenum_link() so I have removed the query string component and re-add it with 'add_args'. Like so:

<?php echo paginate_links(array(
     'base' => preg_replace('/\?.*/', '/', get_pagenum_link(1)) . '%_%',
     'current' => max(1, get_query_var('paged')),
     'format' => 'page/%#%',
     'total' => $wp_query->max_num_pages,
     'add_args' => array(
         's' => get_query_var('s'),
         'post_type' => get_query_var('post_type'),
     )
 )); ?>

The following works for pages with any query-vars. If you visit
example.com/blog/?foo=bar&baz the link for the next page will have the same query-vars set: example.com/blog/page/1/?foo=bar&baz.

$link_unescaped = get_pagenum_link( 1, false ); // esc=false so parse_url works.
$url_components = wp_parse_url( $link_unescaped );
$add_args       = array();
if ( isset( $url_components['query'] ) ) {
    wp_parse_str( $url_components['query'], $add_args ); // $add_args is updated.
}

echo paginate_links(
    array(
        'base'      => strtok( $link_unescaped, '?' ) . '%_%',
        'format'    => 'page/%#%/',
        'current'   => max( 1, get_query_var( 'paged' ) ),
        'total'     => $query->max_num_pages,
        'add_args'  => $add_args,
    )
);
发布评论

评论列表(0)

  1. 暂无评论