简体   繁体   中英

how to remove blog from custom post type url in wordpress?

The custom post type URL is created using toolset plugin. We have post named faq. for example: the link should be www.example.com/faq but the link in my side is www.example.com/blog/faq I tried to fix it using rewrite condition in function.php

'rewrite'=>array('with_front'=> false,'slug'=>'blog');

Try this method to remove blog in your URL

<?php 
function custom_post_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( 'blog') );
    }
}
add_action( 'pre_get_posts', 'custom_post_request' );
function remove_custom_slug( $post_link, $post, $leavename ) {

    if ( 'blog' != $post->post_type  || 'blog' != $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_slug', 10, 3 );

?>

You just need to remove blog and put '/' in rewrite rule of custom post type of FAQ.

'rewrite'=>array('with_front'=> false,'slug'=>'/');

Let me know does it working or not?

Adding 'rewrite'=>array('with_front'=> false,'slug'=>'/');

works for the CTP but breaks the regular blog PT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM