去除分类和自定义分类链接里的名字版本二完整版

<?php
/**
 * Remove taxonomy base from permalink URLs
 * This file is meant to be included in a theme or plugin
 */

/**
 * Remove taxonomy base from permalink URL for all taxonomies
 * 
 * @param string $permalink The full permalink.
 * @param object $term The term object.
 * @param string $taxonomy The taxonomy name.
 * @return string Modified permalink without taxonomy base.
 */
function remove_taxonomy_base($permalink, $term, $taxonomy) {
    // Get all public taxonomies
    $public_taxonomies = get_taxonomies(array('public' => true), 'names');
    
    // Check if current taxonomy is in our list of public taxonomies
    if (in_array($taxonomy, $public_taxonomies)) {
        // Special handling for default category taxonomy
        if ($taxonomy === 'category') {
            // Get the category base or use default 'category'
            $category_base = get_option('category_base');
            $category_base = $category_base ? $category_base : 'category';
            
            // Remove the base from the permalink
            $permalink = str_replace('/' . $category_base . '/', '/', $permalink);
        } else {
            // For other taxonomies, create clean permalink
            $permalink = home_url('/' . $term->slug . '/');
        }
    }
    
    return $permalink;
}
add_filter('term_link', 'remove_taxonomy_base', 10, 3);

/**
 * Advanced rewrite rules to prevent 404 errors for all taxonomies
 */
function advanced_taxonomy_rewrite_rules() {
    // Get all public taxonomies
    $public_taxonomies = get_taxonomies(array('public' => true), 'names');
    
    // Process each taxonomy
    foreach ($public_taxonomies as $taxonomy) {
        // Get terms for this taxonomy
        $terms = get_terms(array(
            'taxonomy' => $taxonomy,
            'hide_empty' => false,
        ));
        
        // Create specific rewrite rules for each term
        if (!empty($terms) && !is_wp_error($terms)) {
            foreach ($terms as $term) {
                // Skip if the term slug might conflict with a potential post type or other reserved slugs
                if (is_reserved_term_slug($term->slug)) {
                    continue;
                }
                
                // Different handling based on taxonomy type
                if ($taxonomy === 'category') {
                    // For default categories, use 'category_name' in the query
                    add_rewrite_rule(
                        '^' . $term->slug . '/?$',
                        'index.php?category_name=' . $term->slug,
                        'top'
                    );
                    
                    // Pagination for categories
                    add_rewrite_rule(
                        '^' . $term->slug . '/page/([0-9]{1,})/?$',
                        'index.php?category_name=' . $term->slug . '&paged=$matches[1]',
                        'top'
                    );
                    
                    // Handle hierarchical categories
                    if ($term->parent > 0) {
                        $ancestors = get_term_parents_list($term->term_id, 'category', array(
                            'format' => 'slug',
                            'separator' => '/',
                            'link' => false,
                            'inclusive' => false
                        ));
                        
                        if (!empty($ancestors)) {
                            add_rewrite_rule(
                                '^' . trim($ancestors, '/') . '/' . $term->slug . '/?$',
                                'index.php?category_name=' . $term->slug,
                                'top'
                            );
                            
                            add_rewrite_rule(
                                '^' . trim($ancestors, '/') . '/' . $term->slug . '/page/([0-9]{1,})/?$',
                                'index.php?category_name=' . $term->slug . '&paged=$matches[1]',
                                'top'
                            );
                        }
                    }
                } else {
                    // For custom taxonomies
                    add_rewrite_rule(
                        '^' . $term->slug . '/?$',
                        'index.php?' . $taxonomy . '=' . $term->slug,
                        'top'
                    );
                    
                    // Pagination for custom taxonomies
                    add_rewrite_rule(
                        '^' . $term->slug . '/page/([0-9]{1,})/?$',
                        'index.php?' . $taxonomy . '=' . $term->slug . '&paged=$matches[1]',
                        'top'
                    );
                    
                    // Handle hierarchical taxonomies
                    if (is_taxonomy_hierarchical($taxonomy) && $term->parent > 0) {
                        $ancestors = get_term_parents_list($term->term_id, $taxonomy, array(
                            'format' => 'slug',
                            'separator' => '/',
                            'link' => false,
                            'inclusive' => false
                        ));
                        
                        if (!empty($ancestors)) {
                            add_rewrite_rule(
                                '^' . trim($ancestors, '/') . '/' . $term->slug . '/?$',
                                'index.php?' . $taxonomy . '=' . $term->slug,
                                'top'
                            );
                            
                            add_rewrite_rule(
                                '^' . trim($ancestors, '/') . '/' . $term->slug . '/page/([0-9]{1,})/?$',
                                'index.php?' . $taxonomy . '=' . $term->slug . '&paged=$matches[1]',
                                'top'
                            );
                        }
                    }
                }
            }
        }
    }
}
add_action('init', 'advanced_taxonomy_rewrite_rules', 10);

/**
 * Check if a slug is reserved by WordPress core, plugins, or post types
 *
 * @param string $slug The term slug to check.
 * @return boolean True if the slug is reserved.
 */
function is_reserved_term_slug($slug) {
    // Common reserved WordPress slugs
    $reserved_slugs = array(
        'attachment', 'attachment_id', 'author', 'author_name', 'calendar',
        'cat', 'category', 'category_name', 'cpage', 'day', 'debug', 'error',
        'exact', 'feed', 'hour', 'link_category', 'm', 'minute', 'monthnum',
        'more', 'name', 'nav_menu_item', 'nopaging', 'offset', 'order', 'orderby',
        'p', 'page', 'page_id', 'paged', 'pagename', 'pb', 'post', 'post_type',
        'posts', 'preview', 'robots', 's', 'search', 'second', 'sentence',
        'tag', 'tag_id', 'taxonomy', 'tb', 'term', 'terms', 'theme', 'title',
        'type', 'w', 'year', 'comments_popup', 'admin', 'login', 'wp-admin',
        'wp-content', 'wp-includes', 'wp-json', 'comments', 'shop'
    );
    
    // Get all registered post types
    $post_types = get_post_types(array('public' => true), 'names');
    
    // Merge reserved slugs with post types
    $all_reserved = array_merge($reserved_slugs, $post_types);
    
    return in_array($slug, $all_reserved);
}

/**
 * Helper function to flush rewrite rules when needed
 */
function flush_taxonomy_rules() {
    advanced_taxonomy_rewrite_rules();
    flush_rewrite_rules();
}

/**
 * Add the flush rewrite rules action to admin_init for flushing when needed
 * This is a safer approach than flushing on every page load
 */
function maybe_flush_taxonomy_rules() {
    if (get_option('taxonomy_base_removal_flush_needed', 'yes') === 'yes') {
        flush_taxonomy_rules();
        update_option('taxonomy_base_removal_flush_needed', 'no');
    }
}
add_action('admin_init', 'maybe_flush_taxonomy_rules');

/**
 * Set flush flag when saving permalinks in admin or when a term is added/edited/deleted
 */
function set_taxonomy_flush_needed() {
    update_option('taxonomy_base_removal_flush_needed', 'yes');
}
add_action('created_term', 'set_taxonomy_flush_needed');
add_action('edited_term', 'set_taxonomy_flush_needed');
add_action('delete_term', 'set_taxonomy_flush_needed');
add_action('permalink_structure_changed', 'set_taxonomy_flush_needed');
×
去除分类和自定义分类链接里的名字版本二完整版

去除分类和自定义分类链接里的名字版本二完整版

<?php /** * Remov...