admin管理员组文章数量:1387460
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 badges1 Answer
Reset to default 0WordPress 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).
版权声明:本文标题:How can I programmatically change the permalink structure of a custom post type in WordPress - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744576233a2613642.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论