admin管理员组

文章数量:1290927

Alternate Question: How to change Permalink structure for CPT so that post name comes in the middle of URL instead of in the end eg. /a/%postname%/b/c

Any suggestions while changing the permalink structure in custom post type? Right now I want the struct to be /a/%postname%/b/c but it keeps adding postname in the end by default like /a/%postname%/b/c/%postname% .

Here's my register_post_type code,

$args = array(
    'label' => __('testCPT', 'text_domain'),
    'description' => __('Site articles.', 'text_domain'),
    'labels' => $labels,
    'supports' => array('title', 'revisions'),
    'hierarchical' => false,
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 5,
    'show_in_admin_bar' => true,
    'show_in_nav_menus' => true,
    'can_export' => true,
    'has_archive' => true,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'rewrite' => array(
        'slug' => '/a/%postname%/b/c',
        'with_front'    => false
    ),
);

Structure generated:

http://localhost/project/a/yoda/b/c/yoda/

Structure Desired:

http://localhost/project/a/yoda/b/c/

Alternate Question: How to change Permalink structure for CPT so that post name comes in the middle of URL instead of in the end eg. /a/%postname%/b/c

Any suggestions while changing the permalink structure in custom post type? Right now I want the struct to be /a/%postname%/b/c but it keeps adding postname in the end by default like /a/%postname%/b/c/%postname% .

Here's my register_post_type code,

$args = array(
    'label' => __('testCPT', 'text_domain'),
    'description' => __('Site articles.', 'text_domain'),
    'labels' => $labels,
    'supports' => array('title', 'revisions'),
    'hierarchical' => false,
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 5,
    'show_in_admin_bar' => true,
    'show_in_nav_menus' => true,
    'can_export' => true,
    'has_archive' => true,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'rewrite' => array(
        'slug' => '/a/%postname%/b/c',
        'with_front'    => false
    ),
);

Structure generated:

http://localhost/project/a/yoda/b/c/yoda/

Structure Desired:

http://localhost/project/a/yoda/b/c/

Share Improve this question edited Jun 4, 2021 at 8:34 mach2 asked Jun 4, 2021 at 8:19 mach2mach2 1134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Note that you should use %<post type>% in the slug value, so if the post type is test_cpt, then you would use %test_cpt% as in /a/%test_cpt%/b/c.

And moving the post name/slug to the middle or beginning in the permalink structure is actually pretty easy:

  1. Register the post type:

    // replace test_cpt with the correct post type
    register_post_type( 'test_cpt', $args );
    
  2. Then after that, do this:

    global $wp_rewrite;
    // replace test_cpt with the correct post type
    $wp_rewrite->extra_permastructs['test_cpt']['struct'] = '/a/%test_cpt%/b/c';
    

And remember to flush the rewrite rules which can be done just by visiting the permalink settings page.

本文标签: custom post typesWhy does wordpress keep adding postname to my CPT39s permalink in the end