Snippet displaying LearnDash parent course title with lesson title
-
20-05-2021 - |
質問
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.
解決
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.
所属していません wordpress.stackexchange