我正在使用WordPress中的自定义帖子类型,对Slug有一个问题。
假设我们需要创建带有分类测验类别的自定义post类型测验。我使用了以下代码:
// Create and register custom post type quiz
function create_posttype_quiz() {
$labels = array(
\'name\' => _x( \'Quiz\', \'post type general name\' ),
\'singular_name\' => _x( \'Quiz\', \'post type singular name\' ),
\'add_new\' => _x( \'Add New\', \'quiz\' ),
\'add_new_item\' => __( \'Add New Quiz\' ),
\'edit_item\' => __( \'Edit Quiz\' ),
\'new_item\' => __( \'New Quiz\' ),
\'all_items\' => __( \'All Quiz\' ),
\'view_item\' => __( \'View Quiz\' ),
\'search_items\' => __( \'Search Quiz\' ),
\'not_found\' => __( \'No quiz found\' ),
\'not_found_in_trash\' => __( \'No quiz found in the Trash\' ),
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Quizzes\'
);
$args = array(
\'labels\' => $labels,
\'description\' => \'Holds our quiz and quiz specific data\',
\'public\' => true,
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'editor\' ),
\'has_archive\' => true,
\'rewrite\' => array(\'slug\' => \'quiz\'),
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
);
register_post_type( \'quiz\', $args );
}
add_action( \'init\', \'create_posttype_quiz\' );
// Create and register quiz categories
function create_taxonomies_quiz() {
$labels = array(
\'name\' => _x( \'Quiz Categories\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Quiz Category\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Quiz Categories\' ),
\'all_items\' => __( \'All Quiz Categories\' ),
\'parent_item\' => __( \'Parent Quiz Category\' ),
\'parent_item_colon\' => __( \'Parent Quiz Category:\' ),
\'edit_item\' => __( \'Edit Quiz Category\' ),
\'update_item\' => __( \'Update Quiz Category\' ),
\'add_new_item\' => __( \'Add New Quiz Category\' ),
\'new_item_name\' => __( \'New Quiz Category\' ),
\'menu_name\' => __( \'Quiz Categories\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
//\'show_admin_column\'=> true,
\'rewrite\' => array(\'hierarchical\'=>true, \'slug\'=>\'quizzes\', \'with_front\'=>false),
);
register_taxonomy( \'quiz_category\', \'quiz\', $args );
}
add_action( \'init\', \'create_taxonomies_quiz\', 0 );
一切正常,我能够创建和编辑测验,将它们添加到测验类别等。此外,我还可以访问前端的测验,例如网站。com/测验/样本测验一,站点。com/测验/样本测验二。。。并在测验类别中列出测验,例如网站。com/测验/第一类,网站。com/测验/第二类。。。
问题是我不能把测验列在下面site.com/quizzes 但在site.com/quiz 相反(site.com/quizzes 将创建找不到页面错误)。
基本上,我希望能够:
访问url上的单个测验:site.com/quiz/sample-quiz-one列出所有测验site.com/quizzes 而不是site.com/quiz.如果不使用htaccess或任何其他重定向,但使用上面的代码(可能是我不知道的一些参数),是否可以实现这一点?