Snippet #
Important: All snippets are provided as-is without support or guarantees. These snippets are provided as guidelines for advanced users looking to customize LearnDash. For any additional help or support with these snippets, we recommend reaching out to a LearnDash Expert.
This code creates a shortcode [ld_course_steps_list] to display a list of lessons, topics, or quizzes in a LearnDash course (without links).
Usage:
All Steps: [ld_course_steps_list course_id=”123″ type=”all”]
Lessons Only: [ld_course_steps_list course_id=”123″ type=”lessons”][>
Replace 123 with the course ID.
add_shortcode('ld_course_steps_list', function($atts) {
$atts = shortcode_atts(
[
'course_id' => 0, // Required: Course ID
'type' => 'all', // Optional: 'lessons', 'topics', 'quizzes', or 'all'
],
$atts,
'ld_course_steps_list'
);
$course_id = (int) $atts['course_id'];
$type = strtolower($atts['type']);
if (!$course_id) return 'Please provide a valid course ID.';
// Allowed types for filtering
$allowed_types = ['all', 'lessons', 'topics', 'quizzes'];
if (!in_array($type, $allowed_types)) {
return 'Invalid type. Please use "lessons", "topics", "quizzes", or "all".';
}
// Get all steps in the course
$steps_hierarchy = learndash_get_course_steps($course_id);
if (empty($steps_hierarchy)) return 'No steps found for this course.';
$output = '<ul>';
foreach ($steps_hierarchy as $step_id) {
$step_title = get_the_title($step_id);
$step_type = get_post_type($step_id);
$step_type_label = '';
switch ($step_type) {
case 'sfwd-lessons':
$step_type_label = 'lessons';
break;
case 'sfwd-topic':
$step_type_label = 'topics';
break;
case 'sfwd-quiz':
$step_type_label = 'quizzes';
break;
default:
$step_type_label = 'unknown';
}
// Filter steps by type
if ($type !== 'all' && $type !== $step_type_label) {
continue;
}
$output .= '<li>' . esc_html($step_title) . ' (' . esc_html(ucfirst($step_type_label)) . ')</li>';
}
$output .= '</ul>';
return $output;
});