I want to add a /news/
before every post slug. Like this:
example.com /news/ post-slug/
If I add the /news/
in the permalinks settings, every custom post type is broken after it.
I tried to add the following to every register_post_type
:
'with_front' => false,
But that doesn’t help.
I also tried the code from here:
$post_type = <POST TYPE NAME> # Define your own as a string $new_slug = <WANTED SLUG> # Define your own as a string add_filter( 'register_post_type_args', function($args, $post_type){ if ( 'post_type' == $post_type ) $args['rewrite'] = array( 'slug' => $new_slug ); return $args; }, 10, 2 );
Unfortunately that doesn’t work either.
Is there any way to add the /news/
without breaking the other post types?
Here’s the current code for my custom post type:
add_action( 'init', 'cpt_custom' ); function cpt_custom() { register_post_type( 'custom', array( 'rewrite' => array( 'slug' => 'custom' ), 'hierarchical' => true, 'public' => true, 'has_archive' => true, 'show_in_rest' => true, 'with_front' => true, ) ); }
In the permalink settings it’s set to /news/%postname%/
Advertisement
Answer
I found the answer. You have to add the with_front
attribute inside the rewrite
.
Like this:
'rewrite' => array( 'slug' => 'custom', 'with_front' => false ),