首页 > 解决方案 > 在分页中使用时如何从 WP_Query url 中删除帖子名称?

问题描述

这可能吗?

我正在尝试在我的页面和单页上进行分页。看起来它正在工作,因为它可以识别有多少页面并显示 URL。问题:网址错误!

函数.php:

function pagination_bar( $query_wp ) 
{
    $pages = $query_wp->max_num_pages;
    $big = 999999999; // need an unlikely integer
    if ($pages > 1)
    {
        $page_current = max(1, get_query_var('paged'));
        echo paginate_links(array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => $page_current,
            'total' => $pages,
        ));
    }
}

查询:

$the_query = new WP_Query(  array( 'posts_per_page' => 25,
                                           'paged' => $paged
                                            ) ); 
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

的HTML:

    <nav class="pagination">
<?php pagination_bar( $the_query ); ?>

激活分页并单击下一页时我得到的 URL:

https://example.com/my_postorpagename/page/3/

我想得到什么:

https://example.com/page/3/

问题:(如何)我可以这样做吗?

编辑: 我也尝试像这样编辑它

    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( "https://mywebsitename.com%_%" ) ) ),

但它只给了我我的URL,没有/page/x...

标签: phpwordpresspagination

解决方案


好吧。我通过改变这样的基础让它工作:

从:

'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),

至:

'base' => str_replace( $big, '%#%', "http://example.com/page/$big" ),

推荐阅读