Custom post type archive page not working

I am trying to create a custom post type. All are working fine except archive page. My custom post type archive page is archive-courses.php but is is not working it is showing default archive.php . Is there any wrong in my code?

Can anyone help me, please?

function td_courses() {
    $labels = array(
        'name'               => _x( 'Courses', 'td' ),
        'singular_name'      => _x( 'Courses', 'td' ),
        'add_new'            => _x( 'Add New', 'td' ),
        'add_new_item'       => __( 'Add New course' ),
        'edit_item'          => __( 'Edit course' ),
        'new_item'           => __( 'New courses' ),
        'all_items'          => __( 'Courses' ),
        'view_item'          => __( 'View courses' ),
        'search_items'       => __( 'Search courses' ),
        'not_found'          => __( 'No courses found' ),
        'not_found_in_trash' => __( 'No courses found in the Trash' ),
        'menu_name'          => 'Courses'
    );
    $args = array(
        'labels'            => $labels,
        'public'            => true,
        'menu_position'     => 10,
        'menu_icon'             => 'dashicons-welcome-learn-more',
        'supports'          => array( 'title', 'editor', 'thumbnail', 'author' ),
        'has_archive'       => true,
        'capability_type' => 'page',
        'rewrite'           => array( 'slug' => 'course' ),
    );
    register_post_type( 'courses', $args );

}
add_action( 'init', 'td_courses' );

function td_courses_taxonomies() {
    $labels = array(
        'name'              => _x( 'Course Categories', 'td' ),
        'singular_name'     => _x( 'Course Categories', 'td' ),
        'search_items'      => __( 'Search Course Categorie' ),
        'all_items'         => __( 'All Course Categories' ),
        'parent_item'       => __( 'Parent Course Categorie' ),
        'parent_item_colon' => __( 'Parent Course Categorie:' ),
        'edit_item'         => __( 'Edit Course Categorie' ),
        'update_item'       => __( 'Update Course Categorie' ),
        'add_new_item'      => __( 'Add New Course Categorie' ),
        'new_item_name'     => __( 'New Course Categorie Name' ),
        'menu_name'         => __( 'Course Categorie' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'courses-list' ),
    );

    register_taxonomy( 'courses_category', array( 'courses' ), $args );

}
add_action( 'init', 'td_courses_taxonomies', 0 );

Thanks

2 Answers
2

Your post type, courses, has a rewrite slug of course. So your archive template will be archive-course.php.

Your taxonomy, courses_category, has a rewrite slug of course, so your taxonomy archive template will be taxonomy-courses.php.

This complete template hierarchy can be useful.

If you want an archive page showing all posts of the post type courses at yoursite.com/courses, you should assign the post type a rewrite slug of courses. (I believe this is the default since your post type is courses.)

The taxonomy archive template, taxonomy-[slug].php, will be invoked when viewing an archive page of all posts of the post type courses assigned to a specific taxonomy term. So if you had a term beginner, and the rewrite slug courses for the taxonomy term, you’d see an archive of beginner courses at yoursite.com/courses/beginner.

If you assign the same rewrite slug to the post type and the taxonomy, you may get some clashing. But I’m not sure.

As Pieter Goosen said, you will need to flush the rewrite rules whenever you make changes like this.

Leave a Comment