Add rewrite rule to permalink structure

1. Add a new rewrite rule:

add_action('init', function()
{
    add_rewrite_rule('^dog/([^/]+)/?$', 'index.php?cat=dog&name=$matches[1]', 'top');
}, 10, 0);

2. Filter the post link:

add_filter('post_link', function($post_link, $post, $leave_name = false, $sample = false)
{
    if ( is_object_in_term($post->ID, 'category', 'DOG') ) {
        $post_link = str_replace($post->ID . '-', '', $post_link);
    }

    return $post_link;

}, 10, 4);

Try it in your functions.php. Hope it works for you!

Leave a Comment