There are two ways to implement to remove category: WordPress which covers almost all cases. The common issues we face is with the pagination of WordPress categories. WordPress do not find posts for a category in paginated page.
We come across issues mentioned above, when we try to change permalinks settings by adding dot “.” in category field.
Why does the problem in pagination happens?
Let’s say you have a category named food
.
Now, when you have a category URL like: https://example.com/
food
(instead of https://example.com/category/food
) 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/food/page/2
is a the second page of another page / post, or the second page of a category archive (because food
may very well be a page slug or page
be a post slug under category food
).
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 food
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:
- Go to:
WordPress Admin Panel Menu
→Settings
. - Put a single dot (
.
) in theCategory 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 theCustom Structure
text field. - Click Save Changes button.
1. Solving the Category Pagination Problem via Custom Plugin:
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 or Yoast SEO
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.
2. How to remove the category prefix with Yoast SEO
In Yoast SEO you can remove the category prefix from the category archive URL.
Note: This feature can cause unexpected URL behavior and is not recommended in most cases.
- Log in to your WordPress website. When you’re logged in, you will be in your ‘Dashboard’.
- Click on ‘SEO’. On the left-hand side, you will see a menu. In that menu, click on ‘SEO’.
- Click on ‘Search Appearance’. The ‘SEO’ settings will expand providing you with additional options.
- Click on ‘Search Appearance’.
- Click on the ‘Taxonomies’ tab.
- Toggle the ‘Category URLs’ switch. To include the category prefix, toggle the switch to ‘Keep’.
To exclude the category prefix, toggle the switch to ‘Remove’. - Save your changes.