Have a Custom Post Type index page display taxonomy items instead of posts

I have a CPT registered that is called lessons with a custom taxonomy (category) that is called courses. I want the /courses page (archive-courses.php?) to display the individual courses (i.e. “online marketing”, “paid advertising”) and not the individual posts (lessons).

Would this mean I’d just customize the loop, or a complete custom WP_Query?

Related posts

Leave a Reply

2 comments

  1. If you’d like to list the individual courses, i.e. the taxonomy terms, you’d use neither WP_Query nor the WP standard Loop.

    Instead, make use of the get_terms function to retrieve the courses. It returns an array of term objects (if the taxonomy does exist and has terms matching the function arguments). Iterate over that and do something with it, such as displaying a list of links to the lessons:

    $courses = get_terms( 'courses' );
    
    if ( $courses ) {
        echo '<ul class="course-list">';
    
        foreach ( $courses as $course ) {
    
            echo '<li>' .
                '<a href="/courses/' . $course->slug . '" ' .
                    'title="' . sprintf( 'View lessons of %s', $course->name ) . '">' .
                        $course->name .
                '</a>' .
            '</li>';
    
        }
    
        echo '</ul>';
    }