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.
The problem which is mentioned above happens when we add dot “.” in the category field by making changes to permalinks settings.
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 the next post/webpage, or its a next page of the paginated category.
Because
food
may very well be a page slug orpage
be a post slug under categoryfood
This happens specially because WordPress confuses with /page/2
part as a different multi-part paginated page or post.
So, instead of loading next pages of the food
category archive, WordPress will try to load a paginated post with the slug page
and you’ll get the 404
error and WordPress will not load 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 if you have not done it already, follow the instructions given below to achieve the URL structure:
# URL structure for Categories
https://example.com/category-slug
For this:
- Redirect to:
WordPress Admin Panel Menu
→Settings
. - Add 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:
- Redirect to:
WordPress Admin Panel Menu
→Settings
. - Select the
Custom Structure
and add/%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. ScartchCoding.Dev will not suggest to rely on SEO plugins for fixing this issue as this is a core URL structure issue of WordPress website. So, what if you want to change the SEO plugin anytime in future and the other plugin has better options but don’t provide a solution for this?
Therefore it is better to use a simple plugin that fixes only this problem. You may use the following CODE given below 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 will perfectly work with pagination when you have category URL as below.
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.
To handle these kind of cases, there these two famous plugins to handle and fix this issue for you.
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 you should use the solution that works best for you, but also assure that you don’t try to do the same thing in multiple different plugins.
2. How to remove the category prefix with Yoast SEO
Using Yoast SEO you can remove the category prefix from the category archive URL.
Note: This feature may cause unexpected URL behavior and is not recommended in most cases.
- Log in to your WordPress website. When you’re logged in, you will see your ‘Dashboard’ panel.
- Click on ‘SEO’. On the left-hand side, you will see a menu. Click on ‘SEO’ over here.
- Click on ‘Search Appearance’. The ‘SEO’ settings will expand and show you additional options as given below.
- 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.