Php Wordpress URL重写。删除CPT slug,但保留子页面功能

Php Wordpress URL重写。删除CPT slug,但保留子页面功能,php,wordpress,url-rewriting,Php,Wordpress,Url Rewriting,我希望我的自定义帖子类型products不使用slug,例如:domain.com/product-name。下面的代码很好地解决了这一问题,但是它无法处理子页面,例如:domain.com/product-name/product-name-feature,it 404s 我最初尝试给自定义post类型以“/”的形式重写slug,虽然这对自定义post类型很好,但它杀死了普通页面(404)。因此,如果解决方案是使用“/”然后修复普通页面,也可以 // Post Type function b

我希望我的自定义帖子类型products不使用slug,例如:domain.com/product-name。下面的代码很好地解决了这一问题,但是它无法处理子页面,例如:domain.com/product-name/product-name-feature,it 404s

我最初尝试给自定义post类型以“/”的形式重写slug,虽然这对自定义post类型很好,但它杀死了普通页面(404)。因此,如果解决方案是使用“/”然后修复普通页面,也可以

// Post Type

function base_types() {

    $labels = array(
        'name'               => 'Products & Features',
        'singular_name'      => 'Product',
        'add_new'            => 'Add New',
        'add_new_item'       => 'Add New Product / Feature',
        'edit_item'          => 'Edit Product',
        'new_item'           => 'New Product',
        'all_items'          => 'All',
        'view_item'          => 'View Product / Feature',
        'search_items'       => 'Search Product / Feature',
        'not_found'          => 'None found',
        'not_found_in_trash' => 'None found in Trash',
        'parent_item_colon'  => '',
        'menu_name'          => 'Products / Features'
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'product', 'with_front' => false ),
        'capability_type'    => 'page',
        'has_archive'        => false,
        'hierarchical'       => true,
        'menu_position'      => 10,
        'with_front'         => false,
        'menu_icon'          => 'dashicons-chart-bar',
        'supports'           => array( 'title', 'editor', 'author', 'page-attributes' )
    );

    register_post_type( 'product', $args );

}

add_action( 'init', 'base_types' );


// Rewrites

function remove_slug( $post_link, $post, $leavename ) {

    if ( 'product' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }
    if( $post->post_parent ) {

        $parent = get_post($post->post_parent);
        $post_link = str_replace( '/' . $post->post_type . '/' . $parent->post_name . '/', '/' . $parent->post_name . '/', $post_link );

    }

    else {

        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    }
    return $post_link;
}

add_filter( 'post_type_link', 'remove_slug', 10, 3 );


function parse_request( $query ) {

    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'product', 'page' ) );
    }
}

add_action( 'pre_get_posts', 'parse_request' );
第一级页面按预期工作。 子页面获得了正确的永久链接,但它们是404


有人能帮忙或指出正确的方向吗?谢谢。

这是通过使用/%post\u id%的自定义结构解决的/


在其他任何地方都找不到这个答案,所以我自己添加了它。

这是通过使用/%post\u id%的自定义结构解决的/

没有在其他地方找到这个答案,所以我自己加了进去