Custom Permalink Structure For a Specific Category

Question

Hello i wanted to achieve custom permalink for news category and this is my code but its getting 404
The url is showing exactly as i wanted but the url is not redirecting to that specific post.

Desired Permalink Structure is

/%category%/%postname%/%post_id%/

CODE HERE

    //Rewrite URLs for "testimonial" category
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
    // Get the category for the post
    $category = get_the_category($post->ID);
    if (  !empty($category) && $category[0]->cat_name == "News" ) {
        $cat_name = strtolower($category[0]->cat_name);
        $permalink = trailingslashit( home_url("https://wordpress.stackexchange.com/". $cat_name . "https://wordpress.stackexchange.com/" . $post->post_name . "https://wordpress.stackexchange.com/" . $post->ID ."https://wordpress.stackexchange.com/" ) );
    }
    return $permalink;
}

add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
    add_rewrite_rule(
        'news/([^/]+)(?:/([0-9]+))?/?$',
        'index.php?category_name=news&name=$matches[1]&page=$matches[2]',
        'top' // The rule position; either 'top' or 'bottom' (default).
    );
}

Please Help

0
Ayushvam 1 month 2022-10-24T03:43:21-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse