Load Page
when a Page
and a Category
archive have the same URL:
This is default WordPress behaviour: When you have the same URL for a category archive & for a page, WordPress will load the page instead of the category archive.
So unless you have a plugin that is changing this behavior for your WordPress setup, your duplicate URL should load the page, not the category archive.
To achieve the proper URL Structure:
The URL structure you wanted needs some work. So unless you've done it already, follow the instructions below to achieve the said URL structure:
# URL structure for Pages:
https://example.com/page-slug
This happens by default, you don't need to do anything.
# URL structure for Categories
https://example.com/category-slug
For this:
- Go to:
WordPress Admin Panel Menu
→ Settings
.
- Put a single dot (
.
) in the Category base
text field.
- Click Save Changes button.
# URL structure for Posts
https://example.com/category-slug/post-slug
For this:
- Go to:
WordPress Admin Panel Menu
→ Settings
.
- Select
Custom Structure
and enter /%category%/%postname%/
in the Custom Structure
text field.
- Click Save Changes button.
If for some reason you are not getting the expected result from this, then use this URL structure in a WordPress installation with:
- WordPress Core updated to the latest version.
- No plugin is activated
- A default theme like Twentyseventeen is activated
and then see what happens with duplicate Page and Category URL.
Solving the Category Pagination Problem:
If you check this post you'll see that this kind of URL structure will cause some pagination problem in your category archive pages. This happens because WordPress confuses with /page/2
part as a different page or post.
For example: say you have a category named service
and service
category has posts like web development
, hosting
etc. with the following URL:
https://example.com/service
https://example.com/service/web-development
https://example.com/service/hosting
Now you may have multiple pages in your service
category argive page, like:
https://example.com/service
https://example.com/service/page/2
https://example.com/service/page/3
Because of this URL structure, WordPress thinks you are trying to load a post that has the slug post
under the service
category and that post is a paginated post.
So instead of loading the next pages of your service
category archive, WordPress will try to load a paginated post with the slug page
and you'll get a 404
(page not found) error instead.
To fix this, you may use a simple plugin with the following CODE:
<?php
/*
Plugin Name: Category Pagination Fix
Plugin URI: https://wordpress.stackexchange.com/a/308826/110572
Description: Fix category pagination for possible conflicts with page or post url
Version: 1.0.0
Author: Fayaz Ahmed
Author URI: https://www.fayazmiraz.com/
*/
function wpse308326_fix_category_pagination( $query_string = array() )
{
if( isset( $query_string['category_name'] )
&& isset( $query_string['name'] ) && $query_string['name'] == 'page'
&& isset( $query_string['page'] ) ) {
$paged = trim( $query_string['page'], '/' );
if( is_numeric( $paged ) ) {
// we are not allowing 'page' as a page or post slug
unset( $query_string['name'] );
unset( $query_string['page'] ) ;
// for a category archive, proper pagination query string is 'paged'
$query_string['paged'] = ( int ) $paged;
}
}
return $query_string;
}
add_filter( 'request', 'wpse308326_fix_category_pagination' );