2

I have created a Category with the slug service, which has the permalink: https://example.com/service.

and a Page with the link: https://example.com/service.

In the Category service, there is a post with the link like: https://example.com/service/post1

Now the link https://example.com/service is redirecting to the Category Archive, but I want it to load the Page instead.

How can I do that?

  • 1
    May I ask why? What you have done is create complexity on something that should be straightforward. – Drupalizeme Jul 12 '18 at 8:17
0

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:

  1. Go to: WordPress Admin Panel MenuSettings.
  2. Put a single dot (.) in the Category base text field.
  3. Click Save Changes button.

# URL structure for Posts
https://example.com/category-slug/post-slug

For this:

  1. Go to: WordPress Admin Panel MenuSettings.
  2. Select Custom Structure and enter /%category%/%postname%/ in the Custom Structure text field.
  3. 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:

  1. WordPress Core updated to the latest version.
  2. No plugin is activated
  3. 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' );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.