WordPress Post Rebrite Rule将页面导致404页



我需要我的帖子永久链接包含自定义分类学值, type,如这样:

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

article是帖子的type分类学值。我已经开始工作了,但是我遇到的问题是,现在我网站的所有页面均为404。我的自定义帖子类型和普通帖子URL按预期工作,只是损坏的页面URL。这是引起页面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分类学导致页面为404

当我添加自定义帖子键入永久链接重写时,我的常规帖子永久链接停止工作。不能同时使两者都能工作

嗨,我知道这是一个较晚的答案,我今天有这个问题,找到了此信息:

https://rudrastyh.com/wordpress/taxonomy-slug-in-post-type-url.html

荣誉转到:Misha Rudrastyh为此答案很明显,他没有在他的CPT中使用重写声明,而似乎使用过滤器来做同样的事情,这使我超越了所有帖子和第404页。

>

为了引用他的后代代码,他解释说要使用此代码解决该问题:

function rudr_post_type_permalink($permalink, $post_id, $leavename) {
    $post_type_name = 'post_type_name'; // post type name, you can find it in admin area or in register_post_type() function
    $post_type_slug = 'post_type_name'; // the part of your product URLs, not always matches with the post type name
    $tax_name = 'custom_taxonomy_name'; // the product categories taxonomy name
    $post = get_post( $post_id );
    if ( strpos( $permalink, $post_type_slug ) === FALSE || $post->post_type != $post_type_name ) // do not make changes if the post has different type or its URL doesn't contain the given post type slug
        return $permalink;
    $terms = wp_get_object_terms( $post->ID, $tax_name ); // get all terms (product categories) of this post (product)
    if ( !is_wp_error( $terms ) && !empty( $terms ) && is_object( $terms[0] ) ) // rewrite only if this product has categories
        $permalink = str_replace( $post_type_slug, $terms[0]->slug, $permalink );
    return $permalink;
}

add_filter('request', 'rudr_post_type_request', 1, 1 );
function rudr_post_type_request( $query ){
    global $wpdb;
    $post_type_name = 'post_type_name'; // specify your own here
    $tax_name = 'custom_taxonomy_name'; // and here
    $slug = $query['attachment']; // when we change the post type link, WordPress thinks that these are attachment pages
    // get the post with the given type and slug from the database
    $post_id = $wpdb->get_var(
        "
        SELECT ID
        FROM $wpdb->posts
        WHERE post_name = '$slug'
        AND post_type = '$post_type_name'
        "
    );
    $terms = wp_get_object_terms( $post_id, $tax_name ); // our post should have the terms

    if( isset( $slug ) && $post_id && !is_wp_error( $terms ) && !empty( $terms ) ) : // change the query
        unset( $query['attachment'] );
        $query[$post_type_name] = $slug;
        $query['post_type'] = $post_type_name;
        $query['name'] = $slug;
    endif;
    return $query;
}

此外,就像我说的那样,您将不需要声明"重写"。在CPT参数中,您可以离开过滤器来处理"事实之后"。类型的东西。

最新更新