我需要我的帖子永久链接包含自定义分类值 type ,如下所示:

http://staging.mysite.com/article/post-title

article 是该帖子的 type 分类值 . 我遇到的是现在我所有的网站's pages are 404ing. My custom post type and normal post urls work as intended, it'只是页面网址被破坏了 . 以下是导致页面URL问题的代码:

// Type taxonomy (no issues here)
function create_type_taxonomy() {
    register_taxonomy(
        'type',
        'post',
        array(
            'labels' => array(
                'name' => 'Type',
                'add_new_item' => 'Add New Type',
                'new_item_name' => 'New Type'
            ),
            'show_ui' => true,
            'show_tagcloud' => false,
            'hierarchical' => false,
            'rewrite' => array(
                'slug' => 'type',
                'with_front' => true
            ),
        )
    );
}

add_action( 'init', 'create_type_taxonomy', 0 );

add_action( 'init', 'st_default_post_type', 1 );

// Re-register built-in posts with a custom rewrite rule
function st_default_post_type() {
    register_post_type( 'post', array(
        'labels' => array(
            'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
        ),
        'public'  => true,
        '_builtin' => false, 
        '_edit_link' => 'post.php?post=%d', 
        'capability_type' => 'post',
        'map_meta_cap' => true,
        'hierarchical' => false,
        'rewrite' => array( 'slug' => '%type%', 'with_front' => false ), // custom rewrite rule
        'query_var' => false,
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
    ) );
}

add_filter('post_type_link', 'st_posts_permalink_structure', 10, 4);

// Replace custom rewrite rule on posts (%type%) with the taxonomy value
function st_posts_permalink_structure($post_link, $post, $leavename, $sample){
    if ($post->post_type != 'post') {
        var_dump($post->post_type);
        return $post_link;
    }
    else{
        if (strpos($post_link, '%type%') === FALSE){
            return $post_link;
        }

        $post = get_post($post);
        if (!$post){ 
            return $post_link;
        }

        $terms = wp_get_object_terms($post->ID, 'type');

        if ( !is_wp_error($terms) && !empty($terms) && is_object($terms[0]) ){
            $taxonomy_slug = $terms[0]->slug;
        }
        else{
            $taxonomy_slug = 'type';
        }

        return str_replace('%type%', $taxonomy_slug, $post_link);
    }
}

希望另一组眼睛可能会捕获导致页面永久链接为404的内容 . 我已经尝试过将管理中的永久链接设置更改为 /%type%/%postname%/ ,但这有同样的问题 . 我在这里发现了几个其他问题看起来像我有同样的问题,但没有一个问题得到回答:

WordPress Taxonomy Causing Pages to 404

When I add custom post type permalink rewrite, my regular post permalinks stop working. Can't get both to work at the same time