site.com/courses/
- 返回404site.com/courses/science/
- 显示一个存档页面,其中包含该自定义分类法中正确的所有帖子site.com/courses/science/rocket-to-the-moon/
- 显示了单个自定义帖子类型,该类型也正确,我不知道为什么site.com/courses/
返回404而不是显示列出所有自定义帖子类型的存档页。。。?This is the code that I\'ve used:
<?php
/*Courses Custom Post Type*/
function my_custom_post_courses() {
$labels = array(
\'name\' => _x( \'Courses\', \'post type general name\' ),
\'singular_name\' => _x( \'Course\', \'post type singular name\' ),
\'add_new\' => _x( \'New course\', \'reis\' ),
\'add_new_item\' => __( \'Add new course\' ),
\'edit_item\' => __( \'Edit course\' ),
\'new_item\' => __( \'New item\' ),
\'all_items\' => __( \'All courses\' ),
\'view_item\' => __( \'View courses\' ),
\'search_items\' => __( \'Search courses\' ),
\'not_found\' => __( \'Nothing found \' ),
\'not_found_in_trash\' => __( \'Nothing found in the trash\' ),
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Courses\'
);
$args = array(
\'labels\' => $labels,
\'description\' => \'Enter a new course\',
\'public\' => true,
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\' ),
\'has_archive\' => true,
\'hierarchical\' => true,
\'rewrite\' => array(\'slug\' => \'courses/%course-type%\',\'with_front\' => false),
\'query_var\' => true,
//\'rewrite\' => true,
//\'publicly_queryable\' => false,
);
register_post_type( \'courses\', $args );
}
add_action( \'init\', \'my_custom_post_courses\' );
/* Courses custom taxonomy */
function my_taxonomies_course_type() {
$labels = array(
\'name\' => _x( \'Course type\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Course type\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search course types\' ),
\'all_items\' => __( \'All course types\' ),
\'parent_item\' => __( \'Parent course type\' ),
\'parent_item_colon\' => __( \'Parent course type:\' ),
\'edit_item\' => __( \'Edit course type\' ),
\'update_item\' => __( \'Update course type \' ),
\'add_new_item\' => __( \'Add new course type\' ),
\'new_item_name\' => __( \'New course type\' ),
\'menu_name\' => __( \'Course type\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'public\' => true,
\'query_var\' => \'course-type\',
\'rewrite\' => array(\'slug\' => \'courses\' ),
\'_builtin\' => false,
);
register_taxonomy( \'course-type\', \'courses\', $args );
}
add_action( \'init\', \'my_taxonomies_course_type\', 0 );
/* Permalink filter Courses */
add_filter(\'post_link\', \'course_permalink\', 1, 3);
add_filter(\'post_type_link\', \'course_permalink\', 1, 3);
function course_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, \'%course-type%\') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, \'course-type\');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = \'no-course-type\';
return str_replace(\'%course-type%\', $taxonomy_slug, $permalink);
}