Is it possible to change the Wordpress parameter “/page/2” to something else?

▼魔方 西西 提交于 2020-06-13 08:02:24

问题


Disclaimer: I'm working on a legacy corporate WP site (PHP 5.3.3), and I can't make very many major breaking changes.

I am adding pagination to an existing resources page (/resources). I'm fairly new to Wordpress in general, but I do have my page working and displaying the correct number of resources on page load.

My question is: when you paginate in Wordpress using:

<?php next_posts_link( 'More Resources' , $the_query->max_num_pages ); ?>

It creates a link on the page, and when you click it, WP automagically adds the page param like so: /resources/page/2/. The issue I'm having is that I get a 404 the minute I click it. I've done a ton of reading on this, and the main reason this happens usually is when you make a custom query using wp_query, and the number of posts in the custom query differs from the one in the main page query. I thought this was the issue originally, so I moved the logic to set the posts_per_page param into a pre_post_load hook, and this is functioning fine:

function exp_wp_modify_main_query ( $query ) {

    if ( $query->is_page( 'resources' ) && $query->is_main_query() ) { // Run only on resources page
        $query->query_vars['posts_per_page'] = 8;
    }

}

add_action( 'pre_get_posts', 'exp_wp_modify_main_query' );

I do have enough blog posts to require pagination (there are more posts than my number of posts per page). I believe the issue is related to the fact that this page is set up without categories. The resources page doesn't have categories. So instead of the url looking like this: resources/some_category/page/2/, it's just resources/page/2. Is it possible that WP thinks "pages" is the category and that's why it's throwing a 404?

If yes, is there a way to change /pages/2/ to /p/2/, and if yes to that, is that even a good idea?


回答1:


I figured this out. To change the pagination base:

I made this function in my plugin:

function set_pagination_base () {

    global $wp_rewrite;

    $wp_rewrite->pagination_base = 'p';

}

And then:

add_action( 'init', 'set_pagination_base' );


来源:https://stackoverflow.com/questions/44979496/is-it-possible-to-change-the-wordpress-parameter-page-2-to-something-else

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!