星期三 , 22 1 月 2025

😳待补充 如何去除自定义分类列表页url中的自定义分类名(就像从分类列表页去除category一样)

下面代码待补充子级分类和分类分页时候的重写规则

<?php 
// Remove product_category base from taxonomy permalink
function remove_product_category_base($permalink, $term, $taxonomy) {
    if ($taxonomy === 'product_category') {
        $permalink = str_replace('/product_category/', '/', $permalink);
    }
    return $permalink;
}
add_filter('term_link', 'remove_product_category_base', 10, 3);

// Advanced rewrite rules to prevent 404 errors
function advanced_product_category_rewrite_rules() {
        // Get all product category terms
        // The problem is the broad rewrite rule ^([^/]+)/?$ which can interfere with other WordPress routes. Here's a more precise solution:
    $terms = get_terms(array(
        'taxonomy' => 'product_category',
        'hide_empty' => false
    ));

    // Create specific rewrite rules only for existing product category terms
    if (!empty($terms)) {
        foreach ($terms as $term) {
            add_rewrite_rule(
                '^' . $term->slug . '/?$', 
                'index.php?product_category=' . $term->slug, 
                'top'
            );
        }
    }

    // Flush rewrite rules safely
    flush_rewrite_rules(false);
}
add_action('init', 'advanced_product_category_rewrite_rules', 10);

// Ensure query handling
function custom_product_category_parse_request($wp) {
    // Check if it's a product_category query
    if (isset($wp->query_vars['product_category'])) {
        // Ensure the term exists
        $term = get_term_by('slug', $wp->query_vars['product_category'], 'product_category');
        
        if ($term) {
            // Term exists, proceed normally
            return $wp;
        }
    }

    // If no match, allow default WordPress routing
    return $wp;
}
add_filter('parse_request', 'custom_product_category_parse_request');

// Additional query vars
function product_category_query_vars($query_vars) {
    $query_vars[] = 'product_category';
    return $query_vars;
}
add_filter('query_vars', 'product_category_query_vars');

// Comprehensive permalink handling
function custom_permalink_filter($redirect_url, $requested_url) {
    // Your custom logic can go here if needed
    return $redirect_url;
}
add_filter('redirect_canonical', 'custom_permalink_filter', 10, 2);



// Final safety net for routing
function fallback_product_category_template($template) {
    if (is_tax('product_category')) {
        $template = locate_template('taxonomy-product_category.php');
        
        if (empty($template)) {
            $template = locate_template('archive-product.php');
        }
        
        if (empty($template)) {
            $template = locate_template('archive.php');
        }
    }
    return $template;
}
add_filter('template_include', 'fallback_product_category_template');




//rerwite product from url 
function remove_product_slug($post_link, $post) {
    if ('product' === $post->post_type && 'publish' === $post->post_status) {
        $post_link = home_url('/') . $post->post_name . '/';
    }
    return $post_link;
}
add_filter('post_type_link', 'remove_product_slug', 10, 2);

function custom_rewrite_rules_for_products() {
    add_rewrite_rule(
        '^([^/]+)/?$',
        'index.php?product=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_rewrite_rules_for_products');

function handle_product_slug_query($query) {
    // Skip if it's an admin query or not the main query
    if (is_admin() || !$query->is_main_query()) {
        return;
    }
    
    // Check if the request is for a potential product or page slug
    if (isset($query->query['name']) && !empty($query->query['name'])) {
        $name = $query->query['name'];
        
        // First, check if it's a product
        $product = get_page_by_path($name, OBJECT, 'product');
        if ($product) {
            $query->set('post_type', 'product');
            return;
        }
        
        // If not a product, check other post types
        $post = get_page_by_path($name);
        if ($post) {
            $query->set('post_type', $post->post_type);
        }
    }
}
add_action('pre_get_posts', 'handle_product_slug_query');

function add_product_query_var($vars) {
    $vars[] = 'product';
    return $vars;
}
add_filter('query_vars', 'add_product_query_var');


function custom_template_hierarchy($template) {

   if (is_home()) {
        $home_template = locate_template('home.php');
        if ($home_template) {
            return $home_template;
        }
    }

    // Check if we're on a single page
    if (is_singular()) {
        $post_type = get_post_type();
        
        // For product post type, use single-product.php if it exists
        if ($post_type === 'product') {
            $custom_template = locate_template('single-product.php');
            if ($custom_template) {
                return $custom_template;
            }
        }
        
        // For pages, ensure page.php is used
        if ($post_type === 'page') {

             $post = get_queried_object();
            
            // 1. Check for custom page template set in the page editor
            $custom_template = get_page_template_slug($post->ID);
            if ($custom_template) {
                $custom_template_path = locate_template($custom_template);
                if ($custom_template_path) {
                    return $custom_template_path;
                }
            }
            
            // 2. Check for page-{slug}.php
            if ($post->post_name) {
                $slug_template = locate_template('page-' . $post->post_name . '.php');
                if ($slug_template) {
                    return $slug_template;
                }
            }
            
            // 3. Check for page-{id}.php
            $id_template = locate_template('page-' . $post->ID . '.php');
            if ($id_template) {
                return $id_template;
            }

             // 4. Check for front page
            if (is_front_page()) {
                $front_page_template = locate_template('front-page.php');
                if ($front_page_template) {
                    return $front_page_template;
                }
            }
            
          
            // 6. Default page template
            $page_template = locate_template('page.php');
            if ($page_template) {
                return $page_template;
            }
        }
    }
    
    return $template;
}
add_filter('template_include', 'custom_template_hierarchy');


// Additional helper to ensure home.php is used for posts page
function custom_home_page_template($template) {
    if (is_home()) {
        $home_template = locate_template('home.php');
        if ($home_template) {
            return $home_template;
        }
    }
    return $template;
}
add_filter('home_template', 'custom_home_page_template');