0

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

  • Have you flushed your permalinks? – Pieter Goosen Jul 4 '16 at 9:25
  • No, I didn't do anything with the permalink. – Khandaker Ikrama Jul 4 '16 at 9:36
  • 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 Jul 4 '16 at 9:39
  • Ok. Done. But still same problem. :( – Khandaker Ikrama Jul 4 '16 at 9:43
  • I see I missed that, you cannot have the same rewrite rule for your post type and taxonomy – Pieter Goosen Jul 4 '16 at 9:50
3

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.

  • I actually did not even checked the rewrite slugs when I skimmed through the code. In all probability, the issue is the rewrite slug. You can have the same slug for your post type and taxonomy, but it would require quite a lot of extra work. I know I answered such a question a while back where we used a plugin to sort the issue with the rewrite rules. In short, one should really avoid having the same rewrite slugs, – Pieter Goosen Jul 4 '16 at 9:53
  • Strange, I have changed my archive template name to archive-course.php but still facing the same issue. Also, change my taxonomies rewrite slug to courses-list . but no luck :( – Khandaker Ikrama Jul 4 '16 at 10:07
  • update your question with what you have now, it is hard to know whether you need to use course or courses? – majick Jul 4 '16 at 10:15
  • Just update the code. Please check now. – Khandaker Ikrama Jul 4 '16 at 10:28
  • 1
    According to the code you posted, the archive-courses.php template should be displayed when you visit yoursite.com/course. If it's not, then you probably haven't successfully flushed your rewrite rules, or you have other rules that are interfering with your CPT's archive rules. – NateWr Jul 4 '16 at 12:00
0

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.

  • It might be helpful to include an example of how you did that. – butlerblog Dec 17 '18 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.