要修改WordPress自定义文章类型(Custom Post Type)的固定链接结构,您可以使用WordPress提供的register_post_type
函数来实现。以下是一个示例,展示如何创建一个名为“books”的自定义文章类型,并修改其固定链接结构:
function custom_post_type_books() { $labels = array( 'name' => 'Books', 'singular_name' => 'Book', 'menu_name' => 'Books', 'all_items' => 'All Books', 'add_new' => 'Add New', 'add_new_item' => 'Add New Book', 'edit_item' => 'Edit Book', 'new_item' => 'New Book', 'view_item' => 'View Book', 'search_items' => 'Search Books', 'not_found' => 'No books found', 'not_found_in_trash' => 'No books found in trash', ); $args = array( 'labels' => $labels, 'public' => true, 'has_archive' => true, 'publicly_queryable' => true, 'query_var' => true, 'rewrite' => array('slug' => 'custombooks'), // 这里修改了固定链接结构 'capability_type' => 'post', 'hierarchical' => false, 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'customfields'), ); register_post_type('books', $args); } add_action('init', 'custom_post_type_books');
在上述示例中,我们通过'rewrite'
参数来修改了自定义文章类型“books”的固定链接结构。在这里,我将固定链接结构设置为'custombooks'
,您可以根据需要将其更改为您想要的任何结构。
在修改完固定链接结构后,不要忘记刷新WordPress的固定链接设置,以确保修改生效。要执行此操作,请登录到WordPress仪表板,转至“设置” > “固定链接”并点击“保存更改”。
请注意,这个示例是基于WordPress截止日期为2021年9月的知识,可能会受到后续WordPress版本的更改影响。在实际使用时,最好查阅最新的WordPress文档以确保正确的实现。