我在设置永久链接结构时遇到了一些麻烦:

www.mysite.com/some-constant-term/%custom-tax-term-slug%/%custom-post-name%/

我在插件中这样做,我正在设置我的CPT:

add_action('init', 'message_board_cpt');
function message_board_cpt()
    {
        $cap_type = 'post';
        $plural = 'Message Board Subjects';
        $single = 'Message Board Subject';
        $cpt_name = 'mmp-mb-subject';
        $opts['can_export'] = TRUE;
        $opts['capability_type'] = $cap_type;
        $opts['description'] = 'These posts will serve as the subjects for the message board.';
        $opts['exclude_from_search'] = TRUE;
        $opts['has_archive'] = FALSE;
        $opts['hierarchical'] = FALSE;
        $opts['map_meta_cap'] = TRUE;
        $opts['menu_icon'] = 'dashicons-exerpt-view';
        $opts['menu_position'] = 25;
        $opts['public'] = TRUE;
        $opts['publicly_querable'] = FALSE;
        $opts['query_var'] = FALSE;
        $opts['register_meta_box_cb'] = '';
        $opts['rewrite'] = true;
        $opts['show_in_admin_bar'] = TRUE;
        $opts['show_in_menu'] = TRUE;
        $opts['show_in_nav_menu'] = TRUE;

        $opts['labels']['add_new'] = esc_html__( "Add New {$single}", 'wisdom' );
        $opts['labels']['add_new_item'] = esc_html__( "Add New {$single}", 'wisdom' );
        $opts['labels']['all_items'] = esc_html__( $plural, 'wisdom' );
        $opts['labels']['edit_item'] = esc_html__( "Edit {$single}" , 'wisdom' );
        $opts['labels']['menu_name'] = esc_html__( $plural, 'wisdom' );
        $opts['labels']['name'] = esc_html__( $plural, 'wisdom' );
        $opts['labels']['name_admin_bar'] = esc_html__( $single, 'wisdom' );
        $opts['labels']['new_item'] = esc_html__( "New {$single}", 'wisdom' );
        $opts['labels']['not_found'] = esc_html__( "No {$plural} Found", 'wisdom' );
        $opts['labels']['not_found_in_trash'] = esc_html__( "No {$plural} Found in Trash", 'wisdom' );
        $opts['labels']['parent_item_colon'] = esc_html__( "Parent {$plural} :", 'wisdom' );
        $opts['labels']['search_items'] = esc_html__( "Search {$plural}", 'wisdom' );
        $opts['labels']['singular_name'] = esc_html__( $single, 'wisdom' );
        $opts['labels']['view_item'] = esc_html__( "View {$single}", 'wisdom' );

        register_post_type( strtolower( $cpt_name ), $opts );
        flush_rewrite_rules();
    }

设置我的自定义分类:

add_action('init', 'message_board_tax');
function message_board_tax()
    {
        $labels = array(
            'name'                       => __( 'Message Board Categories', 'mmp-mb-subject' ),
            'singular_name'              => __( 'Message Board Category', 'mmp-mb-subject' ),
            'menu_name'                  => __( 'Message Board Categories', 'mmp-mb-subject' ),
            'edit_item'                  => __( 'Edit Message Board Category', 'mmp-mb-subject' ),
            'update_item'                => __( 'Update Message Board Category', 'mmp-mb-subject' ),
            'add_new_item'               => __( 'Add New Message Board Category', 'mmp-mb-subject' ),
            'new_item_name'              => __( 'New Message Board Category Name', 'mmp-mb-subject' ),
            'parent_item'                => __( 'Parent Message Board Category', 'mmp-mb-subject' ),
            'parent_item_colon'          => __( 'Parent Message Board Category:', 'mmp-mb-subject' ),
            'all_items'                  => __( 'All Message Board Categories', 'mmp-mb-subject' ),
            'search_items'               => __( 'Search Message Board Categories', 'mmp-mb-subject' ),
            'popular_items'              => __( 'Popular Message Board Categories', 'mmp-mb-subject' ),
            'separate_items_with_commas' => __( 'Separate message board categories with commas', 'mmp-mb-subject' ),
            'add_or_remove_items'        => __( 'Add or remove message board categories', 'mmp-mb-subject' ),
            'choose_from_most_used'      => __( 'Choose from the most used message board categories', 'mmp-mb-subject' ),
            'not_found'                  => __( 'No message board categories found.', 'mmp-mb-subject' ),
        );
        $args = array(
            'labels'            => $labels,
            'public'            => true,
            'show_in_nav_menus' => true,
            'show_ui'           => true,
            'show_tagcloud'     => true,
            'hierarchical'      => true,
            'rewrite'           => array( 'slug' => 'cpt-constant/%mmp-message-board-tax%' ),
//            'rewrite'           => true,
//            'rewrite'           => false,
            'show_admin_column' => true,
//            'query_var'         => true,
        );
        $args = apply_filters( 'mb_subject_category_args', $args );

        register_taxonomy('mmp-message-board-tax', 'mmp-mb-subject', $args);
        flush_rewrite_rules();
    }

然后我创建一个分类法重写规则,如下所示:

add_action('generate_rewrite_rules', 'generate_taxonomy_rewrite_rules');
generate_taxonomy_rewrite_rules($wp_rewrite)
    {
        $rules = array();
        $post_types = get_post_types( array( 'name' => 'mmp-mb-subject', 'public' => true, '_builtin' => false ), 'objects' );
        $taxonomies = get_taxonomies( array( 'name' => 'mmp-message-board-tax', 'public' => true, '_builtin' => false ), 'objects' );

        foreach ( $post_types as $post_type ) {
            $post_type_name = $post_type->name; // 'developer'
            $post_type_slug = $post_type->rewrite['slug']; // 'developers'

            foreach ( $taxonomies as $taxonomy ) {
                if ( $taxonomy->object_type[0] == $post_type_name ) {
                    $terms = get_categories( array( 'type' => $post_type_name, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0 ) );
                    foreach ( $terms as $term ) {
                        $rules[$post_type_slug . '/' . $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug;
                    }
                }
            }
        }
        $wp_rewrite->rules = $rules + $wp_rewrite->rules;

//        $rules = array();
//        $terms = get_terms( array(
//            'taxonomy' => 'resource_type',
//            'hide_empty' => false,
//        ) );
//
//        $post_type = 'mmp-mb-subject';
//        foreach ($terms as $term) {
//
//            $rules['cpt-constant/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '=$matches[1]&name=$matches[1]';
//
//        }
//        // merge with global rules
//        $wp_rewrite->rules = $rules + $wp_rewrite->rules;
    }

最后我为我的cpt设置了一个重写规则:

add_action('post_type_link', 'change_message_board_cpt_link',10,2);
change_message_board_cpt_link( $permalink, $post ) {

        if( $post->post_type == 'mmp-mb-subject' ) {
            $resource_terms = get_the_terms( $post, 'mmp-message-board-tax' );
            $term_slug = '';
            if( ! empty( $resource_terms ) ) {
                foreach ( $resource_terms as $term ) {
                    // The featured resource will have another category which is the main one
//                    if( $term->slug == 'featured' ) {
//                        continue;
//                    }
                    $term_slug = $term->slug;
                    break;
                }
            }
            $permalink = get_home_url() ."/cpt-constant/" . $term_slug . '/' . $post->post_name;
        }
        return $permalink;
    }

我能够进入我的分类页面(所有不同术语的列表),但当我尝试去个别帖子时,我得到了404?