最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I programmatically change the permalink structure of a custom post type in WordPress - Stack Overflow

programmeradmin3浏览0评论

I’m building a custom WordPress site and have created a custom post type using the register_post_type() function. The issue I’m facing is that I want to change the default permalink structure for this custom post type, but I'm not sure where or how to do this properly.

I have the following code to register my custom post type:

function create_custom_post_type() {
    $args = array(
        'labels' => array(
            'name' => 'Books',
            'singular_name' => 'Book',
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array( 'slug' => 'books' ),
        'show_in_rest' => true,
    );
    register_post_type( 'book', $args );
}
add_action( 'init', 'create_custom_post_type' );

By default, the permalinks are being generated as /book/[post-name]. However, I want to change the permalink structure so that it follows the format /library/[post-name].

I’ve already tried modifying the rewrite argument like this:

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

But it didn’t work. The permalinks are still showing as /book/[post-name].

Could anyone point out what I'm doing wrong or how to properly modify the permalink structure for a custom post type? Any help would be greatly appreciated!

Thanks in advance!

I’m building a custom WordPress site and have created a custom post type using the register_post_type() function. The issue I’m facing is that I want to change the default permalink structure for this custom post type, but I'm not sure where or how to do this properly.

I have the following code to register my custom post type:

function create_custom_post_type() {
    $args = array(
        'labels' => array(
            'name' => 'Books',
            'singular_name' => 'Book',
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array( 'slug' => 'books' ),
        'show_in_rest' => true,
    );
    register_post_type( 'book', $args );
}
add_action( 'init', 'create_custom_post_type' );

By default, the permalinks are being generated as /book/[post-name]. However, I want to change the permalink structure so that it follows the format /library/[post-name].

I’ve already tried modifying the rewrite argument like this:

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

But it didn’t work. The permalinks are still showing as /book/[post-name].

Could anyone point out what I'm doing wrong or how to properly modify the permalink structure for a custom post type? Any help would be greatly appreciated!

Thanks in advance!

Share Improve this question asked Mar 17 at 6:52 Anto NavisAnto Navis 4210 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

WordPress caches rewrite rules, so changes to permalink structures don’t take effect immediately.

  • Modify the rewrite Argument Properly
    Ensure your code is correct like this:
function create_custom_post_type() {
    $args = array(
        'labels' => array(
            'name'          => 'Books',
            'singular_name' => 'Book',
        ),
        'public'       => true,
        'has_archive'  => true,
        'rewrite'      => array( 'slug' => 'library', 'with_front' => false ),
        'show_in_rest' => true,
    );
    register_post_type( 'book', $args );
}
add_action( 'init', 'create_custom_post_type' );
  • The 'slug' => 'library' ensures your post type uses /library/[post-name].

  • Adding 'with_front' => false ensures that WordPress does not prepend the default permalink structure.

  • Flush Rewrite Rules
    After modifying the permalink structure, you must flush the rewrite rules. You can do this by:

  • Going to Settings → Permalinks in the WordPress dashboard and simply clicking "Save Changes" (this flushes the rules).

发布评论

评论列表(0)

  1. 暂无评论