admin管理员组

文章数量:1125080

I'm seeking assistance with a custom permalink issue I'm facing on my WordPress website. I've been trying to implement custom permalinks for posts belonging to specific categories, namely "Special Interview" and "Top Consultant," but haven't had much success. Here's what I've attempted so far:

function custom_category_permalink($permalink, $post, $leavename) {
    // Get the post's categories
    $categories = get_the_category($post->ID);
    if ($categories) {
        foreach ($categories as $category) {
            // Check if the post is in "Special Interview" or "Top Consultant" category
            if ($category->slug === 'special-interview' || $category->slug === 'top-consultant') {
                // Append "magazine" to the permalink
                $permalink = trailingslashit(get_site_url()) . 'magazine/' . $post->post_name . '/';
                break; // Stop loop once the condition is met
            }
        }
    }
    return $permalink;
}
add_filter('post_link', 'custom_category_permalink', 10, 3);
add_filter('post_type_link', 'custom_category_permalink', 10, 3);

I updated this code on my functions.php and it did work for a while but few minutes later, the links result in a "Page Not Found" error. I did clear all my cache, did a Permalink Refresh but it did not work. I wonder what is the issue.

Your help will be greatly appreciated! Thank you!

本文标签: Custom permalink structure for posts in specific category not working