【问题标题】:WordPress plugin pagination not working correctlyWordPress插件分页无法正常工作
【发布时间】:2013-07-24 13:25:01
【问题描述】:

我创建了一个使用内置函数 paginate_links 的函数。

但由于 URL 写入错误,分页无法正常工作。

我想要的网址是这样的domain.com/properties/page/2/?foo=bar

正在输出的 URL 是 domain.com/properties/?foo=bar/page/2/

这是我的代码

function paginate($max_num_pages) {
    global $wp_query, $wp_rewrite;
    $wp_query->query_vars['page'] > 1 ? $current = $wp_query->query_vars['page'] : $current = 1;
    $pagination = array(
        'base' => @add_query_arg('page','%#%'),
        'format'       => '',
        'total'        => $max_num_pages,
        'current'      => $current,
        'show_all'     => true,
        'end_size'     => 1,
        'mid_size'     => 2,
        'prev_next'    => True,
        'prev_text'    => __('« Previous'),
        'next_text'    => __('Next »'),
        'type'         => 'plain',
        'add_args'     => false,
        'add_fragment' => ''
    );

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

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

    echo paginate_links( $pagination );
}

【问题讨论】:

  • 将 PHP 代码放在 Stackoverflow 上时,不要使用 @ 错误控制运算符。改为显示所有错误和警告。此外,您的问题看起来像一个 URL 处理问题,为什么不考虑使用轻量级 URL 类,如 Net_URL2 或一些 WP API 函数来处理 URL 并将字符串添加到您感兴趣的 Path part of the URL 中呢?
  • 这里也缺少一个问题。你写你所做的,你写你想要实现的,你写发生的事情。到目前为止,这很好,但是,您没有解释为什么要具体编写该代码,然后您错过了提出一个问题以及为什么您想知道它失败的原因。例如。用你自己的话为什么解释你期望正确的输出。 paginate_links 的哪些参数以及您的哪一行代码应该 建立它?从您的代码和使用的函数的文档(codex)中不清楚您为什么真正期望它。为利润添加更多背景。

标签: php wordpress pagination


【解决方案1】:

这就是最终对我有用的东西。

我不得不将$wp_query->query_vars 更改为paged,并删除了检查我是否使用永久链接if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' ); 的行

function paginate($max_num_pages) {
    global $wp_query, $wp_rewrite;
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
    $pagination = array(
        'base'  => @add_query_arg('paged','%#%'),
        'format'       => '',
        'total'        => $max_num_pages,
        'current'      => $current,
        'show_all'     => true,
        'end_size'     => 1,
        'mid_size'     => 2,
        'prev_next'    => True,
        'prev_text'    => __('« Previous'),
        'next_text'    => __('Next »'),
        'type'         => 'plain',
        'add_args'     => false,
        'add_fragment' => ''
    );

    //if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
    if ( !empty($wp_query->query_vars['s']) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) );  
    echo paginate_links( $pagination );
}

【讨论】:

    【解决方案2】:

    根据the documentation for paginate_links,默认format 看起来与您所追求的相似,但您通过设置'format' => '' 覆盖了该值。

    尝试删除该选项或将其设置为您所追求的:'format' => '?foo=bar%#%'

    【讨论】:

    • 我不是 Wordpress 开发人员,但我觉得解决方案肯定与paginate_linksbaseformat 选项有关。祝你好运。
    • 我同意。我正在深入研究这一点。感谢您的回复。
    • 这不是正确的答案,但我感谢您的时间和精力,所以我给您赏金。
    猜你喜欢
    • 2016-07-29
    • 2017-01-08
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    相关资源
    最近更新 更多