Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nextab/64bcd5cfa175dc6467509df05bb31b0a to your computer and use it in GitHub Desktop.
Save nextab/64bcd5cfa175dc6467509df05bb31b0a to your computer and use it in GitHub Desktop.
#region Auto Generate Menu for Online Courses
// This function (version 2 of the course menu) automatically builds a menu for an online course
function nxt_kursmenu_shortcode($atts, $content = null) {
$a = shortcode_atts( array(
'class' => '',
'menu_id' => '',
), $atts );
$final_tax_id = 0;
if(is_tax('module')) {
// $tax_id = get_queried_object_id();
$nxt_tax = get_queried_object();
}
elseif(is_singular('lesson')) {
$nxt_temp_tax = get_the_terms(get_the_ID(), 'module');
$nxt_tax = $nxt_temp_tax[0];
}
if(empty($nxt_tax->parent)) {
$final_tax_id = $nxt_tax->term_id;
} else {
$final_tax_id = $nxt_tax->parent;
}
// echo '<pre>'; print_r($final_tax_id); echo '</pre>';
if($final_tax_id == 0) {
return;
} else {
// Output wrapper + top level link ("Kursmenü")
$return_string = '<div class="kursmenu-wrapper ' . $a["class"] . '"><ul id="kursmenu" class="menu ">';
// $return_string = '<div class="kursmenu-wrapper ' . $a["class"] . '"><ul id="kursmenu" class="menu "><li class="kurs menu-item menu-item-type-taxonomy current-lesson-ancestor"><a href="' . get_term_link($final_tax_id, 'module') . '">Kursübersicht</a></li>';
// walk over all sub-taxonomies
$termchildren = get_term_children($final_tax_id, 'module');
$sortarray = [];
foreach ( $termchildren as $termchild ) {
$sortarray[$termchild] = get_field('nxt_order_no', 'category_' . $termchild);
}
asort($sortarray);
// echo '<pre>'; print_r($sortarray); echo '</pre>';
foreach ( $sortarray as $child => $sa_value ) {
$term = get_term_by( 'id', $child, 'module' );
$term_progress = count_checked_lessons($child);
$learned = "unlearned";
$open_toggle = 'off';
if( get_queried_object_id() == $child) {
$current_item = "current-menu-item";
$open_toggle = 'on';
} elseif($nxt_tax->term_id == $child) {
$current_item = "current-menu-category";
$open_toggle = 'on';
} else {
$current_item = '';
}
// print_r($term_progress);
if($term_progress[0] == $term_progress[1] && $term_progress[0] > 0) $learned = "learned";
$return_string .= '<div class="menu-item menu-item-type-taxonomy menu-item-object-module menu-item-has-children ' . $learned . ' ' . $current_item . '">';
if($term_progress[0] > 0) {
/* $term_lessons = get_posts([
'post_type' => 'lesson',
'numberposts' => -1,
'tax_query' => [
[
'taxonomy' => 'module',
'field' => 'term_id',
'terms' => $child,
'include_children' => false,
],
],
'post_status' => 'publish',
'posts_per_page' => 99,
'orderby' => 'date',
'order' => 'ASC',
]); */
global $wpdb;
// Prepare your SQL query
$lesson_query = "SELECT ID FROM {$wpdb->posts}
INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)
INNER JOIN {$wpdb->term_taxonomy} ON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id)
WHERE {$wpdb->posts}.post_status = 'publish'
AND {$wpdb->posts}.post_type = 'lesson'
AND {$wpdb->term_taxonomy}.taxonomy = 'module'
AND {$wpdb->term_taxonomy}.term_id = %d
ORDER BY {$wpdb->posts}.post_date ASC";
// Execute the query with the $child variable
$term_lessons_ids = $wpdb->get_col($wpdb->prepare($lesson_query, $child));
// Now $term_lessons_ids contains an array of post IDs
$lesson_output = '<ul class="sub-menu">';
foreach($term_lessons_ids as $single_id) {
$learned = (lesson_checked($single_id)) ? 'learned' : 'unlearned';
$current_item = (get_queried_object_id() == $single_id) ? "current-menu-item" : "";
$lesson_output .= '<li class="menu-item menu-item-type-post_type menu-item-object-lesson ' . $learned . ' ' . $current_item . '"><a href="' . get_the_permalink($single_id) . '">' . get_the_title($single_id) . '</a></li>';
}
$lesson_output .= '</ul>';
$return_string .= do_shortcode('[et_pb_toggle open="' . $open_toggle . '" _builder_version="4.5.8" _module_preset="default" title="' . $term->name . '" hover_enabled="0"]' . $lesson_output . '[/et_pb_toggle]');
} else {
// $return_string .= '<a href="' . get_term_link($term, 'module') . '" title="' . $term->name . '">' . $term->name . '</a>';
$return_string .= $term->name;
}
$return_string .= '</div> <!-- .menu-item -->';
}
$return_string .= '</ul></div>';
}
return $return_string;
}
#endregion Auto Generate Menu for Online Courses
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment