Last active
May 6, 2024 10:08
-
-
Save DumahX/527974caeb8ccc09e2569111320b087d to your computer and use it in GitHub Desktop.
Displays a list of all published MemberPress courses.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function mpcs_all_courses($atts) { | |
$content = ''; | |
$courses = get_posts( | |
array( | |
'post_type' => 'mpcs-course', | |
'post_status' => 'publish', | |
'posts_per_page' => '-1', | |
'orderby' => 'title', | |
'order' => 'ASC' | |
) | |
); | |
if(is_array($courses)) { | |
$content .= '<ul>'; | |
if(has_post_thumbnail($course->ID)) { | |
$content .= get_the_post_thumbnail($course->ID, 'thumbnail'); | |
$content .= '<br>'; | |
} | |
foreach($courses as $course) { | |
$content .= '<li>'; | |
$content .= '<a href="' . get_permalink($course->ID) . '">'; | |
$content .= $course->post_title; | |
$content .= '</a>'; | |
$content .= '</li>'; | |
} | |
$content .= '</ul>'; | |
} | |
return $content; | |
} | |
add_shortcode('mpcs-all-courses', 'mpcs_all_courses'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@DumahX hey, thanks for sharing the code snippet but it arise warning in L 17, it shows undefined variable $course with attempt to read property "ID" on null. Also there are missing few others checking before showing any course.
Thanks btw.