From 9977644d2a1e676d5602cbd28909bd425caff9c4 Mon Sep 17 00:00:00 2001 From: Adam Wood <1017872+adamwoodnz@users.noreply.github.com> Date: Fri, 17 May 2024 16:29:17 +1200 Subject: [PATCH] 2024 Theme: Update local nav Make Learning Pathways menu dynamic; only add terms which have Courses --- .../themes/pub/wporg-learn-2024/functions.php | 75 ++++++++++--------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/wp-content/themes/pub/wporg-learn-2024/functions.php b/wp-content/themes/pub/wporg-learn-2024/functions.php index c325d0c72..fbdd4ce0c 100644 --- a/wp-content/themes/pub/wporg-learn-2024/functions.php +++ b/wp-content/themes/pub/wporg-learn-2024/functions.php @@ -89,40 +89,47 @@ function update_prism_css_path( $path ) { * Provide a list of local navigation menus. */ function add_site_navigation_menus( $menus ) { - return array( - 'learn' => array( - array( - 'label' => __( 'Learning Pathways', 'wporg-learn' ), - 'url' => '/learning-pathways/', - ), - array( - 'label' => __( 'User', 'wporg-learn' ), - 'url' => '/learning-pathway/user/', - ), - array( - 'label' => __( 'Designer', 'wporg-learn' ), - 'url' => '/learning-pathway/designer/', - ), - array( - 'label' => __( 'Contributor', 'wporg-learn' ), - 'url' => '/learning-pathway/contributor/', - ), - array( - 'label' => __( 'Developer', 'wporg-learn' ), - 'url' => '/learning-pathway/developer/', - ), - array( - 'label' => __( 'Courses', 'wporg-learn' ), - 'url' => '/courses/', - ), - array( - 'label' => __( 'Lessons', 'wporg-learn' ), - 'url' => '/lessons/', - ), - array( - 'label' => __( 'Online Workshops', 'wporg-learn' ), - 'url' => '/online-workshops/', - ), + $menu = array( + array( + 'label' => __( 'Courses', 'wporg-learn' ), + 'url' => '/courses/', ), + array( + 'label' => __( 'Lessons', 'wporg-learn' ), + 'url' => '/lessons/', + ), + array( + 'label' => __( 'Online Workshops', 'wporg-learn' ), + 'url' => '/online-workshops/', + ), + ); + + $learning_pathways = get_terms( + array( + 'taxonomy' => 'learning-pathway', + 'hide_empty' => true, + ) + ); + + if ( empty( $learning_pathways ) || is_wp_error( $learning_pathways ) ) { + $menus['learn'] = $menu; + + return $menus; + } + + $learning_pathways_menu = array( + 'label' => __( 'Learning Pathways', 'wporg-learn' ), + 'submenu' => array_map( function( $term ) { + return array( + 'label' => $term->name, + 'url' => get_term_link( $term ), + ); + }, $learning_pathways ), ); + + array_unshift( $menu, $learning_pathways_menu ); + + $menus['learn'] = $menu; + + return $menus; }