Making WordPress.org


Ignore:
Timestamp:
06/06/2024 02:52:02 PM (2 years ago)
Author:
renyot
Message:

Learn: Sync with git WordPress/learn@f6d5835

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-learn-2024/functions.php

    r13775 r13781  
    1515add_action( 'after_setup_theme', __NAMESPACE__ . '\setup' );
    1616add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
     17add_filter( 'wporg_block_site_breadcrumbs', __NAMESPACE__ . '\set_site_breadcrumbs' );
    1718add_filter( 'wporg_block_navigation_menus', __NAMESPACE__ . '\add_site_navigation_menus' );
    1819add_filter( 'single_template_hierarchy', __NAMESPACE__ . '\modify_single_template' );
     
    214215        return $content[ $learning_pathway ];
    215216}
     217
     218/**
     219 * Filters breadcrumb items for the site-breadcrumb block.
     220 *
     221 * @param array $breadcrumbs
     222 *
     223 * @return array
     224 */
     225function set_site_breadcrumbs( $breadcrumbs ) {
     226        if ( isset( $breadcrumbs[0] ) ) {
     227                // Change the title of the first breadcrumb to 'Home'.
     228                $breadcrumbs[0]['title'] = 'Home';
     229        }
     230
     231        $post_type = get_post_type();
     232
     233        if ( is_singular() && 'page' !== $post_type && 'post' !== $post_type ) {
     234                // CPT single page: Insert the archive breadcrumb into the second position.
     235                $post_type_object = get_post_type_object( $post_type );
     236                $archive_title = $post_type_object->labels->name;
     237                $archive_url = get_post_type_archive_link( $post_type );
     238
     239                $archive_breadcrumb = array(
     240                        'url' => $archive_url,
     241                        'title' => $archive_title,
     242                );
     243
     244                array_splice( $breadcrumbs, 1, 0, array( $archive_breadcrumb ) );
     245
     246                // If it's a lesson single page, change the second breadcrumb to the course archive
     247                // and insert the lesson course breadcrumb into the third position.
     248                if ( is_singular( 'lesson' ) ) {
     249                        $lesson_course_id = get_post_meta( get_the_ID(), '_lesson_course', true );
     250
     251                        if ( empty( $lesson_course_id ) ) {
     252                                return $breadcrumbs;
     253                        }
     254
     255                        $post_type_object = get_post_type_object( 'course' );
     256                        $archive_title = $post_type_object->labels->name;
     257                        $archive_url = get_post_type_archive_link( $post_type );
     258
     259                        $archive_breadcrumb = array(
     260                                'url' => $archive_url,
     261                                'title' => $archive_title,
     262                        );
     263
     264                        $lesson_course_title = get_the_title( $lesson_course_id );
     265                        $lesson_course_link = get_permalink( $lesson_course_id );
     266                        $lesson_course_breadcrumb = array(
     267                                'url' => $lesson_course_link,
     268                                'title' => $lesson_course_title,
     269                        );
     270
     271                        $breadcrumbs[1] = $archive_breadcrumb;
     272                        array_splice( $breadcrumbs, 2, 0, array( $lesson_course_breadcrumb ) );
     273                }
     274        }
     275
     276        return $breadcrumbs;
     277}
Note: See TracChangeset for help on using the changeset viewer.