从自定义帖子类型中删除插件

时间:2017-11-17 作者:wushu06

我正在尝试从permalink中删除自定义帖子名称,这段代码对于一篇自定义帖子效果很好,但我正在尝试删除另外3篇自定义帖子的slug。谁能帮我重写它来处理许多自定义帖子,而不是一个。

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

    if ( ! in_array( $post->post_type, array( \'internal_doors\' ) ) || \'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\', \'internal_doors\', \'page\' ) );
}
add_action( \'pre_get_posts\', \'vipx_parse_request_tricksy\' );

1 个回复
最合适的回答,由SO网友:Lovin Nagi 整理而成

if ( ! in_array( $post->post_type, array( \'internal_doors\' ) ) || \'publish\' != $post->post_status )
将上述内容替换为

 if ( ! in_array( $post->post_type, array( \'internal_doors\',\'custom post type name1\', \'custom post type name2\' ) ) || \'publish\' != $post->post_status )
以及

if ( ! empty( $query->query[\'name\'] ) )
        $query->set( \'post_type\', array( \'post\', \'internal_doors\', \'page\' ) );
将此替换为

if ( ! empty( $query->query[\'name\'] ) )
        $query->set( \'post_type\', array( \'post\', \'internal_doors\',\'custom post type name1\', \'custom post type name2\', \'page\' ) );
注意:使用原始名称更改自定义帖子类型名称1、自定义帖子类型名称2

结束