Snippet displaying LearnDash parent course title with lesson title

The question:

I am using LearnDash on my WordPress site, and have created the following snippet to get a list of all lessons:

function get_list_of_lessons_class(){
$args = array(
    'post_type' => 'sfwd-lessons',
    'posts_per_page' => -1,
    'post_status' => 'publish',
    );
$q = new WP_Query($args);
    if ($q->have_posts()) : 
        while ($q->have_posts()) :  $q->the_post();
        the_title();
        endwhile; 
    else:
        ;
        wp_reset_postdata();
    endif;
return $finalout;
}
add_shortcode('get_list_of_lessons', 'get_list_of_lessons_class');

Can anyone advise please how to display the course title of each lesson beside the lesson title?

Thank you.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

You can get the current post’s course ID from learndash_get_course_id(), e.g.

$course_id = learndash_get_course_id();
if (isset($course_id) && $course_id) echo esc_html(get_the_title($course_id));

Under the covers it’s stored in a post meta value course_id, but you might as well use their helper function to read it.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment