6

For example, I have a category /wiki/ https://buhehe.de/wiki/

It has removed the term "category" from the URL, but please see: https://buhehe.de/wiki/page/2

It doesn't show the second category page, but it actually has a second page.

Where is the problem?

4

Why does the problem in pagination happens?

Say you have a category named wiki.

Now, when you have a category URL like: https://example.com/wiki (instead of https://example.com/category/wiki) and say page or post URL like:

https://example.com/a-page
or
https://example.com/category-slug/a-post

WordPress has really no way of knowing if https://example.com/wiki/page/2 is a the second page of another page / post, or the second page of a category archive (because wiki may very well be a page slug or page be a post slug under category wiki).

This happens specifically because WordPress confuses with /page/2 part as a different multi-part paginated page or post.

So, instead of loading the next pages of your wiki 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 of getting the second page of the category archive.

Before I give you the solution to this issue, first make sure you've got the permalink correct.

To achieve the necessary Category 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 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.

Solving the Category Pagination Problem:

Some SEO plugins have solution to this problem. However, since this is a core URL structure issue of your site, it's better not to depend on SEO plugins for this. Say, what if you want to change the SEO plugin in future and the other plugin has better options but don't provide solution for this?

So it's better to use a simple plugin that fixes only this one problem. You may use the following CODE and save it in your plugin directory with file name category-pagination-fix.php and then activate the plugin named Category Pagination Fix from the site's admin panel:

<?php
/*
Plugin Name:  Category Pagination Fix
Plugin URI:   https://wordpress.stackexchange.com/a/311858/110572
Description:  Fix category pagination
Version:      1.0.0
Author:       Fayaz Ahmed
Author URI:   https://www.fayazmiraz.com/
*/

function wpse311858_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', 'wpse311858_fix_category_pagination' );

Alternate solution:

The above solution works perfectly with pagination when you have category URL like:

https://example.com/category-slug/

and post URL like:

https://example.com/category-slug/post-slug

However, when you have subcategory URL like:

https://example.com/category-slug/sub-category-slug

or, when you have post URL like:

https://example.com/post-slug

the above simple solution doesn't work.

In these sort of cases, you may use a plugin like Remove Category URL.

These sort of plugins make some fundamental URL rewrite changes, that's why some default WordPress behaviour may not work as expected after you use one of these plugins. So use the solution that works best for you, but make sure that you don't try to do the same thing in multiple different plugins.

Caution: Using these sort of headless archive URL (e.g. no category or similar term for category page links, no tag or similar term in for tag page links etc.) creates internal conflicts with page & post URL(s). WordPress engine needs a lot of internal checking to determine which to load and which not to. That process is resource hungry. So if your site has thousands of pages, posts, categories, tags etc. it's better to use these terms in the URL.

2

Yoast Plugin - Remove Categories

You could use an SEO plugin such as Yoast which has a remove categories option, or you could program the function yourself which is explained in some of the below links.

Some related posts that might help you:

How to remove category from wordpress url?

Remove category & tag base from WordPress url - without a plugin

  • 1
    Related links are wrong. The first one is about title, not URL. The last one has old & incomplete solutions. – Scott Aug 20 '18 at 6:22
  • Thanks Scott. I've removed the first one. I've left the last one as it has related and useful info within :) – Cryptomanic Aug 20 '18 at 14:41
0

Here is my solution.

This works paginated page too in my case.

Please give it a try and let me know if it works.

// Remove category from URL
add_filter('user_trailingslashit', 'wpse311858_remove_category_from_url');
function wpse311858_remove_category_from_url($link) {
  return str_replace("/category/", "/", $link);
}

// Add new category URLs
add_filter('generate_rewrite_rules', 'wpse311858_category_rewrite');
function wpse311858_category_rewrite($wp_rewrite) {
  $new_rules = [];

  // Get all the categories including empty categories
  $cats = get_categories(['exclude' => 1, 'hide_empty' => false ]); //exclude uncategorized catagory if there were 

  // Loop through each category to add new rules to core rewrite pattern
  foreach( $cats as $cat ) {
    $cat_nicename = $cat->slug;

    // Check if it is parent or child category
    if( $cat->parent !== 0 ) {
      $cat_nicename = get_category_parents( $cat->parent, false, '/', true ) . $cat_nicename;
    }

    // For each pattern
    $new_rules['(' . $cat_nicename . ')/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]'; // for feed pattern
    $new_rules['(' . $cat_nicename . ')/(.+?)/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]'; // for another feed pattern
    $new_rules['(' . $cat_nicename . ')/(.+?)/embed/?$'] = 'index.php?category_name=$matches[1]&embed=true'; // for embed page
    $new_rules['(' . $cat_nicename . ')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]'; // for paged page
    $new_rules['(' . $cat_nicename . ')/?$'] = 'index.php?category_name=$matches[1]'; // for category archive page

  } //endforeach

  // Finally add new rules
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

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.