Rewrite Rules WordPress Returning Category Page Instead of Individual post

120 Views Asked by At

I'm trying to change the permalink only to one specific category and it works good on the changing, but the content isn't right.

It's showing the categories page with a lot of posts instead of the proper content of that post.

Can someone help me with this?

The code:

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 == "Educational" ) {
        $permalink = trailingslashit( home_url('/Educational/'. $post->post_name .'/' ) );
    }
    return $permalink;
}

add_filter( 'post_link', 'custom_permalink', 10, 3);


function custom_rewrite_rules( $wp_rewrite ) {
    // This rule will will match the post id in %postname%-%post_id% struture
    $new_rules['^Educational/([^/]+)/?$'] = 'index.php?name=$matches[1]';
    $new_rules['^Educational/([^/]+)/?$'] = 'index.php?name=$matches[2]';
    $new_rules['^Educational/?$'] = 'index.php?cat=61';
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    return $wp_rewrite;
}

add_action('generate_rewrite_rules', 'custom_rewrite_rules');

0

There are 0 best solutions below