Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress从自定义帖子中删除Slug_Php_Wordpress - Fatal编程技术网

Php Wordpress从自定义帖子中删除Slug

Php Wordpress从自定义帖子中删除Slug,php,wordpress,Php,Wordpress,我的永久链接结构设置为:%category%/%postname%/ 我想从我的自定义帖子中删除slug,我找到了以下解决方案: function gp_add_cpt_post_names_to_main_query( $query ) { // Bail if this is not the main query. if ( ! $query->is_main_query() ) { return; } // Bail if this

我的永久链接结构设置为:
%category%/%postname%/

我想从我的自定义帖子中删除slug,我找到了以下解决方案:

 function gp_add_cpt_post_names_to_main_query( $query ) {
    // Bail if this is not the main query.
    if ( ! $query->is_main_query() ) {
        return;
    }
    // Bail if this query doesn't match our very specific rewrite rule.
    if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
        return;
    }
    // Bail if we're not querying based on the post name.
    if ( empty( $query->query['name'] ) ) {
        return;
    }
    // Add CPT to the list of post types WP will include when it queries based on the post name.
    $query->set( 'post_type', array( 'post', 'page', 'credit-cards' ) );
}
add_action( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' );
但是,只有当my permalink设置为
/%postname%/


如何使其与:
%category%/%postname%/
structure一起工作?

在function.php中添加以下代码并更新帖子类型

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

    if ( ! in_array( $post->post_type, array( 'event' ) ) || 'publish' != $post->post_status )
        return $post_link;

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

    return $post_link;
     }
add_filter( 'post_type_link', 'vipx_remove_cpt_slug', 10, 3 );

function vipx_parse_request_tricksy( $query ) {

// Only noop the main query
if ( ! $query->is_main_query() )
    return;

// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query )
    || ! isset( $query->query['page'] ) )
    return;

// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) )
    $query->set( 'post_type', array( 'post', 'your_post_type', 'page' ) );
}
    add_action( 'pre_get_posts', 'vipx_parse_request_tricksy' );

获取404,因为我的永久链接设置为
%category%/%postname%/