skip to Main Content

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%/

2

Answers


  1. Chosen as BEST 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 ),
    

  2. You can rewrite the custom post-type slug by adding:

    'rewrite' => array( 'slug' => 'news' ),
    
    

    to your custom post type arguments.
    All posts will now have the ‘news’ permalink without affecting the others.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search