Rewrite rule page url with category

I have enabled the category box for pages with this:

// Add to the admin_init hook of your theme functions.php file 
add_action( 'admin_init', 'page_categories' );
function page_categories() {
    // Add tag metabox to page
    register_taxonomy_for_object_type( 'post_tag', 'page' );
    // Add category metabox to page
    register_taxonomy_for_object_type( 'category', 'page' );
}

And now I would like to have an URl structure of :

domain.com/%category%/%pagename%/

I suppose it has to be a rewrite rule but i am not very good with regular expressions.

1 Answer
1

Just of the top of my head, something along this way might work:

function wpse178647_rewrite() {
   add_rewrite_rule(
    '^([^/]+)/([^/]+)/?$', ''
    'index.php?category_name=$matches[1]&pagename=$matches[2]',
    'top'
    );
}
add_action( 'init', 'wpse178647_rewrite' );

Completely and utterly untested.

Leave a Comment