Reduce permalinks (category or post) that include entire hierarchy of categories to just having the lowest level category.
Reduce permalinks (category or post) that include entire hierarchy of categories to just having the lowest category in the hierarchy.
By default, WordPress replaces the %category% permalink tag in a custom permalink structure with the entire hierarchy of categories for the post’s first matching category. For example, assuming your site has a hierarchical category structure like so:
Applications |_ Desktop |_ Web |_ WordPress
By default, if you have a permalink structure defined as %category%/%year%/%monthnum%/%day%/%postname%
, your post titled “Best Plugins” assigned to the “WordPress” category would have a permalink of:
https://www.example.com/applications/web/wordpress/2008/01/15/best-plugins
If you activate the Single Category Permalink plugin, this would be the permalink generated for the post (and recognized by the blog):
https://www.example.com/wordpress/2008/01/15/best-plugins
In order for a category to be used as part of a post’s permalink structure, %category%
must be explicitly defined in the Settings -> Permalinks admin page as part of a custom structure, i.e. /%category%/%postname%
.
For category links, %category%
is implied to follow the value set as the “Category base” (or the default category base if none is specified). So if your category base is ‘category’, the above example would list posts in the ‘WordPress’ category on this category listing page:
https://www.example.com/category/applications/web/wordpress/
With this plugin activated, that link would become:
https://www.example.com/category/wordpress/
NOTE: The fully hierarchical category and post permalinks will continue to work. The plugin issues a 302 redirect to browsers and search engines pointing them to the shorter URL.
Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage
The plugin exposes one filter for hooking. Code using this filter should ideally be put into a mu-plugin or site-specific plugin (which is beyond the scope of this readme to explain). Less ideally, you could put them in your active theme’s functions.php file.
c2c_single_category_redirect_status (filter)
The ‘c2c_single_category_redirect_status’ hook allows you to specify an HTTP status code used for the redirect. By default this is 302.
Arguments:
Example:
/** * Change the redirection HTTP status to a 302. * * @param int $code The HTTP status code. By default 301. * @return int */ function scp_change_redirect_status( $code ) { return 302; } add_filter( 'c2c_single_category_redirect_status', 'scp_change_redirect_status' );
single-category-permalinks.zip
inside the plugins directory for your site (typically wp-content/plugins/
)%category%
as a permalink tag in the Settings
-> Permalinks
admin options page when defining a custom permalink structureYes, WordPress will still serve the category listings and posts regardless of whether it is of the full category hierarchy format or just the single category format. But do note that WordPress doesn’t perform any sort of redirects; it responds directly to the category/post URL requested.
Whether this plugin is active or not, WordPress treats both types of category links the same. This plugin will however issue redirects for all of the non-canonical category and post permalink pages to point to the single category link version.
In addition to handling custom permalink structures (used to generate permalinks for posts) that contain %category%
, the plugin also shortens category archive links. WordPress by default generates those links in a fully hierarchical fashion which this plugin will reduce to a single category. See the Description section for an example.
Yes.
phpunit/bin/
into tests/
phpunit/
into tests/
$taxonomy
argument of category_link()
to ‘category’ to avoid a PHP warning/errorget_category()
could return null
for an invalid category IDcategory_link()
to reflect that WP_Error
or null
are also possible valuesphpunit/
to house all files related to unit testingbin/
to phpunit/bin/
tests/bootstrap.php
to phpunit/
tests/
to phpunit/tests/
phpunit.xml
to phpunit.xml.dist
per best practicescategory_link()
, post_link()
unset_permalink_structures()
to unset configured permalink structuresFull changelog is available in CHANGELOG.md.