星期三 , 22 1 月 2025

从product单页url移除product的方法

下面这段代码只能做为参考,用是可以用的但是太蠢了。

add_action('init', 'custom_product_rewrite_rules');
function custom_product_rewrite_rules() {
    // Use transient to cache rewrite rules
    $cached_rules = get_transient('custom_product_rewrite_rules');
    
    if (false === $cached_rules) {
        $products = get_posts(array(
            'post_type'      => 'product',
            'posts_per_page' => -1,
            'fields'         => 'ids' // Only get product IDs for performance
        ));
        
        $rules = array();
        foreach ($products as $product_id) {
            $slug = get_post_field('post_name', $product_id);
            $rules[$slug] = 'index.php?product=' . $slug;
        }
        
        // Cache rules for 24 hours
        set_transient('custom_product_rewrite_rules', $rules, DAY_IN_SECONDS);
        $cached_rules = $rules;
    }
    
    // Add cached rules
    foreach ($cached_rules as $slug => $rule) {
        add_rewrite_rule(
            '^' . $slug . '/?$', 
            $rule, 
            'top'
        );
    }
    
    // Flush rewrite rules (use sparingly)
    flush_rewrite_rules();
}

// Invalidate cache when products are updated
add_action('save_post_product', 'invalidate_product_rewrite_rules_cache');
add_action('delete_post', 'invalidate_product_rewrite_rules_cache');
function invalidate_product_rewrite_rules_cache($post_id) {
    if (get_post_type($post_id) === 'product') {
        delete_transient('custom_product_rewrite_rules');
    }
}
// Modify permalink to remove base
add_filter('post_type_link', 'custom_product_permalink', 10, 2);
function custom_product_permalink($permalink, $post) {
    if ($post->post_type === 'product') {
        return home_url($post->post_name);
    }
    return $permalink;
}

最新的方法在这里

function remove_cpt_slug($post_link, $post) {
    if ('your_post_type' !== $post->post_type || 'publish' !== $post->post_status) {
        return $post_link;
    }
    
    // Remove the slug from the URL
    $post_link = str_replace('/' . $post->post_type . '/', '/', $post_link);
    
    return $post_link;
}
add_filter('post_type_link', 'remove_cpt_slug', 10, 2);

function parse_request_for_cpt($query) {
    // Only modify main queries, not admin
    if (!$query->is_main_query() || is_admin()) {
        return;
    }

    // Get registered post types
    $post_type = 'your_post_type';

    // Check if we're on a single post page
    if (isset($query->query['page'])) {
        $page = get_page_by_path($query->query['page']);
        if (!$page) {
            $query->set('post_type', array('post', $post_type));
        }
    }

    // Check if we're on a paginated archive
    if (isset($query->query['paged'])) {
        $query->set('post_type', array('post', $post_type));
    }
}
add_action('pre_get_posts', 'parse_request_for_cpt');

完整版本,去除所有自定义文章类型的slug

function remove_cpt_slug($post_link, $post) {
    // Get all custom post types
    $args = array(
        'public'   => true,
        '_builtin' => false // This excludes default post types
    );
    $post_types = array_keys(get_post_types($args, 'names'));

    if (!in_array($post->post_type, $post_types) || 'publish' !== $post->post_status) {
        return $post_link;
    }
    
    return str_replace('/' . $post->post_type . '/', '/', $post_link);
}
add_filter('post_type_link', 'remove_cpt_slug', 10, 2);

function parse_request_for_cpt($query) {
    if (!$query->is_main_query() || is_admin()) {
        return;
    }

    if (!isset($query->query['name'])) {
        return;
    }

    $path = trim($query->query['name'], '/');
    
    // Get all custom post types
    $args = array(
        'public'   => true,
        '_builtin' => false
    );
    $post_types = array_keys(get_post_types($args, 'names'));
    
    // Check each custom post type for matching post
    foreach ($post_types as $post_type) {
        $custom_post = get_page_by_path($path, OBJECT, $post_type);
        
        if ($custom_post) {
            $query->set('post_type', array($post_type));
            $query->set('name', $path);
            break;
        }
    }
}
add_action('pre_get_posts', 'parse_request_for_cpt');