因此,我目前正在一个客户端网站上工作,试图将一些旧帖子转换为使用新的自定义帖子类型。在此之前,她的网站是以这样一种方式建立的,即她可以在网站上添加带有章节的书籍以及未分类的建议栏。为了添加这两个项目中的任何一个,她会添加一个新帖子,并将其粘贴到特定类别中。如果她想让这篇文章在她的书中以章节的形式出现,她会将其归入“图书”类别的正确子类别。如果她想让帖子出现在她的建议栏中,她会将其添加到“建议”类别中。
该网站的永久链接是这样设置的:当阅读一本书的某一章时,它看起来是这样的
现在,我正试着重新为她做一些准备。我没有简单地添加一篇新帖子并对其进行分类以达到这种效果,而是将这两个不同的领域划分为各自的自定义帖子类型(称为章和列)。在设置新的自定义帖子类型时,我试图将rewrite参数设置为使用以前使用的相同永久链接,但似乎即使删除了类别,永久链接仍由类别保留。我的意思是,我在尝试访问“/书籍/书籍/章节”时收到404,但如果我将重写更改为使用“章节”或其他基础,它就会起作用。
主要是我想知道是否有办法解决这个问题,以及是什么导致了这个问题。这与最初添加CPT时该类别仍然存在这一事实有关,但我不能完全确定。
以下是您可以看到其实际应用的网站:http://dev1.abileneloving.com
以下是我的CPT/海关税/海关永久链接结构代码:
function chapter_post_type_init() {
$labels = array(
\'name\' => \'Chapters\',
\'singular_name\' => \'Chapter\',
\'add_new\' => \'Add New\',
\'add_new_item\' => \'Add New Chapter\',
\'edit_item\' => \'Edit Chapter\',
\'new_item\' => \'New Chapter\',
\'all_items\' => \'All Chapters\',
\'view_item\' => \'View Chapter\',
\'search_items\' => \'Search Chapters\',
\'not_found\' => \'No chapters found\',
\'not_found_in_trash\' => \'No chapters found in Trash\',
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Books\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'books/%book_type%\'),
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => 100,
\'supports\' => array( \'title\', \'editor\', \'author\', \'excerpt\')
);
register_post_type( \'chapter\', $args );
}
add_action( \'init\', \'chapter_post_type_init\' );
function book_tax_init() {
$labels = array(
\'name\' => _x( \'Books\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Book\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Books\' ),
\'all_items\' => __( \'All Books\' ),
\'edit_item\' => __( \'Edit Book\' ),
\'update_item\' => __( \'Update Book\' ),
\'add_new_item\' => __( \'Add New Book\' ),
\'new_item_name\' => __( \'New Book Name\' ),
\'menu_name\' => __( \'Books\' ),
);
$args = array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'books/%book_type%\')
);
register_taxonomy(
\'book\',
\'chapter\',
$args
);
}
add_action( \'init\', \'book_tax_init\' );
add_filter(\'post_type_link\', \'book_permalink_structure\', 10, 4);
function book_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, \'%book_type%\' ) ) {
$book_type_term = wp_get_object_terms( $post->ID, \'book\' );
$post_link = str_replace( \'%book_type%\', array_pop( $book_type_term )->slug, $post_link );
}
return $post_link;
}
Update - New Code
function chapter_post_type_init() {
$labels = array(
\'name\' => \'Chapters\',
\'singular_name\' => \'Chapter\',
\'add_new\' => \'Add New\',
\'add_new_item\' => \'Add New Chapter\',
\'edit_item\' => \'Edit Chapter\',
\'new_item\' => \'New Chapter\',
\'all_items\' => \'All Chapters\',
\'view_item\' => \'View Chapter\',
\'search_items\' => \'Search Chapters\',
\'not_found\' => \'No chapters found\',
\'not_found_in_trash\' => \'No chapters found in Trash\',
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Books\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'books/%book%\'),
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => 100,
\'supports\' => array( \'title\', \'editor\', \'author\', \'excerpt\')
);
register_post_type( \'chapter\', $args );
}
add_action( \'init\', \'chapter_post_type_init\' );
function book_tax_init() {
$labels = array(
\'name\' => _x( \'Books\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Book\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Books\' ),
\'all_items\' => __( \'All Books\' ),
\'edit_item\' => __( \'Edit Book\' ),
\'update_item\' => __( \'Update Book\' ),
\'add_new_item\' => __( \'Add New Book\' ),
\'new_item_name\' => __( \'New Book Name\' ),
\'menu_name\' => __( \'Books\' ),
);
$args = array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'books\')
);
register_taxonomy(
\'book\',
\'chapter\',
$args
);
}
add_action( \'init\', \'book_tax_init\' );
add_filter(\'post_type_link\', \'book_permalink_structure\', 10, 4);
function book_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, \'%book%\' ) ) {
$book_type_term = wp_get_object_terms( $post->ID, \'book\' );
$post_link = str_replace( \'%book%\', array_pop( $book_type_term )->slug, $post_link );
}
return $post_link;
}