php CPT NO Slug

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php CPT NO Slug相关的知识,希望对你有一定的参考价值。


// Remove slug from CPT
// Careful of collisions and permalink must be %postname%
// http://www.itsabhik.com/remove-custom-post-type-slug/ 
// There was an error in the code after $args array: , not ;
// See also http://colorlabsproject.com/tutorials/remove-slugs-custom-post-type-url/

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

	$args = array( 
			'post_type' => 'services', 
			'public'   => true,
			'_builtin' => false );  


	$cpts = get_post_types( $args, 'objects' );

	foreach ( $cpts  as $cpt ) {
		//Replace the slug
		$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
	}

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

function include_cpt_query( $query ) {

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

    // Only loop 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', get_post_types() );
    }
}
add_action( 'pre_get_posts', 'include_cpt_query' );

以上是关于php CPT NO Slug的主要内容,如果未能解决你的问题,请参考以下文章

php 从CPT中删除Slug

php WordPress自定义分类与CPT相同的Slug

使用 CPT slug 修改自定义分类 slug

CPT slug 之前的父永久链接 slug

Wordpress // 删除 URL 的帖子类型 slug

如何更改wordpress中的slug?