Custom post type archive page not working

admin2025-06-05  2

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

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

Share Improve this question edited Jul 4, 2016 at 10:27 Khandaker Ikrama asked Jul 4, 2016 at 9:06 Khandaker IkramaKhandaker Ikrama 111 gold badge1 silver badge6 bronze badges 5
  • 3 Have you flushed your permalinks? – Pieter Goosen Commented Jul 4, 2016 at 9:25
  • No, I didn't do anything with the permalink. – Khandaker Ikrama Commented Jul 4, 2016 at 9:36
  • 1 You need to flush your permalinks when creating a new taonomy or custom post type. Simply vists your permalinks page and click save/update – Pieter Goosen Commented Jul 4, 2016 at 9:39
  • Ok. Done. But still same problem. :( – Khandaker Ikrama Commented Jul 4, 2016 at 9:43
  • I see I missed that, you cannot have the same rewrite rule for your post type and taxonomy – Pieter Goosen Commented Jul 4, 2016 at 9:50
Add a comment  | 

2 Answers 2

Reset to default 6

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/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/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.

As Pieter Goosen said you need to flush your permalinks when creating a new taxonomy or custom post type. Go to your site settings -> permalinks and click on "save button".

This did the trick for me.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749090271a316279.html

最新回复(0)