Custom courses list display

Hi Supporter,
I want custom display courses list when goto slug /courses.
I search and see it define in class-template, not using template *.php in folder themes.
How I can edit it?
Thank you very much.

  • Sajid

    Hello phuongbinh,
    Hope you are doing good today :slight_smile:

    Yes, it renders the content right from plugin's template file called class-template.php file. If you want to customize it then you can do this by using the following filter.

    function coursepress_template_course_list_box_cb( $template, $course_id, $a){
    
    	$course_id = (int) $a['course_id'];
    		$clickable_label = sanitize_text_field( $a['clickable_label'] );
    		$echo = cp_is_true( $a['echo'] );
    		$clickable = cp_is_true( $a['clickable'] );
    		$url = CoursePress_Data_Course::get_course_url( $course_id );
    
    		$course_image = CoursePress_Data_Course::get_setting( $course_id, 'listing_image' );
    		$has_thumbnail = ! empty( $course_image );
    
    		$clickable_link = $clickable ? 'data-link="' . esc_url( $url ) . '"' : '';
    		$clickable_class = $clickable ? 'clickable' : '';
    		$clickable_text = $clickable ? '<div class="clickable-label">' . $clickable_label . '</div>' : '';
    		$button_label = $a['button_label'];
    		$button_link = $url;
    
    		if ( ! empty( $a['override_button_link'] ) ) {
    			$button_link = $a['override_button_link'];
    		}
    
    		$button_text = sprintf( '<a href="%s" rel="bookmark" class="button apply-button apply-button-details">%s</a>', esc_url( $button_link ), $button_label );
    		$instructor_link = $clickable ? 'no' : 'yes';
    		$thumbnail_class = $has_thumbnail ? 'has-thumbnail' : '';
    
    		$completed = false;
    		$student_progress = false;
    		if ( is_user_logged_in() ) {
    			$student_progress = CoursePress_Data_Student::get_completion_data( get_current_user_id(), $course_id );
    			$completed = isset( $student_progress['completion']['completed'] ) && ! empty( $student_progress['completion']['completed'] );
    		}
    		$completion_class = CoursePress_Data_Course::course_class( $course_id );
    		$completion_class = implode( ' ', $completion_class );
    
    		// Add filter to post classes
    
    		// Override button
    		if ( ! empty( $a['override_button_text'] ) && ! empty( $a['override_button_link'] ) ) {
    			$button_text = '<button class="coursepress-course-link" data-link="' . esc_url( $a['override_button_link'] ) . '">' . esc_attr( $a['override_button_text'] ) . '</button>';
    		}
    
    		/**
    		 * schema.org
    		 */
    		$schema = apply_filters( 'coursepress_schema', '', 'itemscope' );
    		$course_title = do_shortcode( sprintf( '[course_title course_id="%s"]', $course_id ) );
    		$course_title = sprintf( '<a href="%s" rel="bookmark">%s</a>', esc_url( $url ), $course_title );
    
    		$template = '<div class="course course_list_box_item course_' . $course_id . ' ' . $clickable_class . ' ' . $completion_class . ' ' . $thumbnail_class . '" ' . $clickable_link . ' ' . $schema .'>
    			[course_thumbnail course_id="' . $course_id . '"]
    			<div class="course-information">
    				<h1>Test</h1>
    				' . $course_title . '
    				[course_summary course_id="' . $course_id . '"]
    				[course_instructors style="list-flat" link="' . $instructor_link . '" course_id="' . $course_id . '"]
    				<div class="course-meta-information">
    					[course_start label="" course_id="' . $course_id . '"]
    					[course_language label="" course_id="' . $course_id . '"]
    					[course_cost label="" course_id="' . $course_id . '"]
    					[course_categories course_id="' . $course_id . '"]
    				</div>' .
    					$button_text . $clickable_text . '
    			</div>
    		</div>
    		';
    
    return $template;
    
    }
    
    add_filter( 'coursepress_template_course_list_box', 'coursepress_template_course_list_box_cb', 10, 3);

    Add above code functions.php file of your child theme or use it as mu-plugin.

    If you go with mu-plugin route then, go to your /wp-content/ folder via FTP, and find the folder called "mu-plugins". If there is no folder with that name, then you'll want to create one. Then, create a file inside that folder, and give the file any name you like and paste the code in there.

    You don't need to activate that plugin, as it will be always be automatically activated. Please be sure to add a PHP start tag at the beginning of the code if you insert this code as a mu-plugin, like this:

    <?php

    Also, I added <h1>Test</h1> for demo purpose to make sure the filter is working and changes are actually taking effect.

    Alternatively, if you want to use your own template, then you can also duplicate the existing archive.php file of your theme, rename it to archive-course.php file and make changes there.

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

  • Sajid

    Hello phuongbinh,
    Hope you are doing good today :slight_smile:

    If you just want to hide some courses from a specific category on all courses page then you can do this by just following code snippet (don't need above posted code if you don't want to edit the output).

    add_action( 'pre_get_posts', 'wpmudev_custom_course_archive' );
    
    function wpmudev_custom_course_archive( $query ) {
    
    		if( '/courses/' == $_SERVER['REQUEST_URI'] && !is_admin() && 'course' == $query->query['post_type'] ){
    
    			$query->set( 'tax_query', array(
    											'relation' => 'AND',
    											array(
    												'taxonomy' => 'course_category',
    												'field'    => 'slug',
    												'terms'    => array( 'wp' ),
    												'operator' => 'NOT IN',
    											)
    										)
    			 );
    
    	}
    
    }

    Please make sure you replace "wp" with actual slug of your category you don't want courses list from on archive page.

    Also, please avoid bumping the thread, this will make the response slower instead of getting a faster response. This is because our system feeds us threads from oldest to newest. So each time you post a reply, it push the thread down in the queue considering it a new post. Always, post one reply and wait for the support staff to get back to you. We will try our best to respond as fast as we can. That is not an excuse but an explanation of how our system works actually.

    Take care and have a nice day :slight_smile:

    Best Regards,
    Sajid - WPMU DEV Support

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.