Attempting to create a custom rewrite rule for any post that has a category of Shop. The posts use /%postname%/
but I want /shop/%postname%/
to appear in the url. Below is what I have but I can't get it working.
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
// Get the categories for the post
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "Shop" ) {
$permalink = trailingslashit( home_url('/' . $post->post_name ) );
}
return $permalink;
}
add_action('generate_rewrite_rules', 'custom_rewrite_rules');
function custom_rewrite_rules( $wp_rewrite ) {
$new_rules['^shop/([^/]*)-([0-9]+)/?'] = 'index.php?postname=$matches[1]';
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite;
}