$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Remove custom post type slug|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Remove custom post type slug

matteradmin9PV0评论

I'm creating a new website based on a theme using custom post type and WordPress 3.2.1.

The problem is that my content was previously using %postname% permalinks and all my SEO is build on this. Using CPT add a slug like :

I tried this rule in the register_post_type function but it doesn't work :

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

I also read this but it's not good for me : Permalinks in Custom Post types

Any idea ?

I'm creating a new website based on a theme using custom post type and WordPress 3.2.1.

The problem is that my content was previously using %postname% permalinks and all my SEO is build on this. Using CPT add a slug like : http://mysite/slug-cpt/postname

I tried this rule in the register_post_type function but it doesn't work :

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

I also read this but it's not good for me : Permalinks in Custom Post types

Any idea ?

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Jul 28, 2011 at 8:59 Aurélien DenisAurélien Denis 113 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

This is a tricky one, I had exactly the same problem but after a lot of debugging and troubleshooting I managed to fix it.

The trick is to add a new rewrite rule:

function book_rewrite_rule() {
    add_rewrite_rule( '(.*?)$', 'index.php?book=$matches[1]', 'top' );
}
add_action( 'after_theme_setup', 'book_rewrite_rule' );

Question is quite old, but issues still exist. The best way to remove the slug from URL is writing a few lines of code in your functions.php file.

Another way to do it is passing the rewrite parameters as follows:

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

But this is not recommended as it causes 404 error in other pages. So you should opt for the solution of getting into your theme's functions.php.

/**
 * Remove the slug from published post permalinks. Only affect our CPT 
though.
 */
function sh_remove_cpt_slug( $post_link, $post, $leavename ) {

    if ( in_array( $post->post_type, array( 'cpt' ) )
        || 'publish' == $post->post_status )
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
        return $post_link;
}
add_filter( 'post_type_link', 'sh_remove_cpt_slug', 10, 3 );

This will remove the slug from the URL for the published post. This will still cause an error since it specifies that only 'post' and 'page' post types can have url without a post-type slug.

Now to teach WP that out CPT will also have URL without slug, we need to get this in our functions.php

function sh_parse_request_tricksy( $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', array( 'cpt' ) );
    }
}
add_action( 'pre_get_posts', 'sh_parse_request_tricksy' );

Ref: https://wordpress.stackexchange/a/320711/98322

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far