• I rarely use categories, so they’re largely a mystery to me, but need to do something for a specific project.

    I’ve enabled pages to also use categories (not sure if they’ll get used, but I wanted flexibility so it’s largely an experiment).

    My permalinks are /notes/%postname%/, and I’m getting category archives at ‘/notes/category/misc/’.

    How do I remove ‘/notes/’… so that category archives are at root-relative ‘/category/misc/’?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Hi gulliver

    Try it with this in your (child) theme’s functions.php file:

    add_filter( 'register_taxonomy_args', 'remove_front_from_category', 10, 3 );
    
    function remove_front_from_category( $args, $taxonomy, $object_type ) {
    
    	if ( 'category' === $taxonomy ) {
    
    		if ( isset( $args['rewrite']['with_front'] ) ) {
    			$args['rewrite']['with_front'] = false;
    		}
    	}
    
    	return $args;
    }

    Re-save your permalink structure (/notes/%postname%/) in wp-admin > Settings > Permalinks

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost. Or create a plugin with the code above.

    Thread Starter gulliver

    (@gulliver)

    Thanks.

    I didn’t realise it was a ‘with front’ thing – similar to CPTs.

    And regarding use of a child theme – again, thanks. (I use my own custom theme anyway, so don’t have the upgrade issue to worry about.)

    Thread Starter gulliver

    (@gulliver)

    UPDATE…
    Can this be further modified to remove ‘category’… so that category archives are at root-relative ‘/misc/’?

    Some of this is theoretical… for posts which are in category ‘misc’ an archive url of ‘notes/misc/’ is ok – but I’m thinking of a scenario where a page might also be in category ‘misc’ and so an url of ‘notes/misc/’ would be unsatisfactory, and should instead be a root-relative ‘/misc/’.

    And I realise there’s potential issues with naming conflicts – as these mods seem to prevent the default WP behavior of adding a ‘-2’ in such instances.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Category and archive urls’ is closed to new replies.