自定义帖子类型重写导致单个帖子404

时间:2019-01-23 11:14:46

标签: wordpress custom-post-type permalinks wp-rewrite

我的博客文章获得404

这是我的CPT代码,如您所见,我已经删除了此CPT上的子弹

<?php
function cptui_register_my_cpts() {

/**
* Post Type: Collections.
*/

$labels = array(
"name" => __( "Collections", "" ),
"singular_name" => __( "Collection", "" ),
"menu_name" => __( "Collections", "" ),
"all_items" => __( "All Collections", "" ),
"add_new" => __( "Add New", "" ),
"add_new_item" => __( "Add New Collection", "" ),
"edit_item" => __( "Edit Collection", "" ),
"new_item" => __( "New Collection", "" ),
"view_item" => __( "View Collection", "" ),
"view_items" => __( "View Collections", "" ),
"search_items" => __( "Search Collections", "" ),
"not_found" => __( "No Collections Found", "" ),
"not_found_in_trash" => __( "No Collections Found In Trash", "" ),
"parent_item_colon" => __( "Parent Collection", "" ),
"featured_image" => __( "Featured Image", "" ),
"set_featured_image" => __( "Set Featured Image", "" ),
"remove_featured_image" => __( "Remove Featured Image", "" ),
"use_featured_image" => __( "Use Featured Image", "" ),
"parent_item_colon" => __( "Parent Collection", "" ),
);

$args = array(
"label" => __( "Collections", "" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"delete_with_user" => false,
"show_in_rest" => false,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => "collection",
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => array( "slug" => "/", "with_front" => false ),
"query_var" => true,
"supports" => array( "title", "editor", "thumbnail", "custom-fields", "page-attributes" ),
"taxonomies" => array( "product-type", "application", "colour", "placement", "material" ),
);

register_post_type( "collection", $args );
}

add_action( 'init', 'cptui_register_my_cpts' );
?>

这是层次结构

http://mywebsite.com/collection/applications/-无需删除父级CPT子弹

删除了CPT子弹

-http://mywebsite.com/applications/
-http://mywebsite.com/applications/benchtops/
-http://mywebsite.com/material/
-http://mywebsite.com/material/granite/

我的问题是,当我转到自己的博客文章时,它会显示404

-http://mywebsite.com/title_of_blog - 404
-http://mywebsite.com/title_of_blog_2 - 404

这是我的Functions.php中的代码

function remove_custom_post_type_slug( $post_link, $post, $leavename ) {
    if ( ! in_array( $post->post_type, array( 'collection' ) ) || 'publish' != $post->post_status )
        return $post_link;
    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    return $post_link;
}
add_filter( 'post_type_link', 'remove_custom_post_type_slug', 10, 3 );

function parse_post_type_request( $query ) {
    if ( ! $query->is_main_query() )
        return;
    if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) )
        return;
    if ( ! empty( $query->query['name'] ) )
        $query->set( 'post_type', array( 'post', 'collection', 'page' ) );
}
add_action( 'pre_get_posts', 'parse_post_type_request' );

非常感谢任何帮助,谢谢

0 个答案:

没有答案