Пользовательский тип переписывания приводит к отдельным сообщениям 404 - PullRequest
0 голосов
/ 23 января 2019

Мои сообщения в блоге получают 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' );

Любая помощь очень ценится, спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...