$this->type ); } $schema = array( '@type' => $this->type, '@id' => Schema_AI_ID_Builder::breadcrumb( $post_id ), 'itemListElement' => array(), ); $items = array(); $position = 1; // Home page is always the first item. $items[] = array( '@type' => 'ListItem', 'position' => $position, 'name' => __( 'Home', 'ai-schema-gen' ), 'item' => home_url( '/' ), ); $position++; // For hierarchical post types (pages), use ancestors. if ( is_post_type_hierarchical( $post->post_type ) ) { $ancestors = get_post_ancestors( $post_id ); if ( ! empty( $ancestors ) ) { // Ancestors are in reverse order (immediate parent first). $ancestors = array_reverse( $ancestors ); foreach ( $ancestors as $ancestor_id ) { $ancestor = get_post( $ancestor_id ); if ( $ancestor ) { $items[] = array( '@type' => 'ListItem', 'position' => $position, 'name' => $ancestor->post_title, 'item' => get_permalink( $ancestor_id ), ); $position++; } } } } else { // For non-hierarchical types (posts), use categories. $categories = get_the_category( $post_id ); if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) { // Use the primary category (first one, or Yoast primary if available). $primary_cat = $this->get_primary_category( $post_id, $categories ); if ( $primary_cat ) { // Build category hierarchy. $cat_hierarchy = $this->get_category_hierarchy( $primary_cat ); foreach ( $cat_hierarchy as $cat ) { $items[] = array( '@type' => 'ListItem', 'position' => $position, 'name' => $cat->name, 'item' => get_category_link( $cat->term_id ), ); $position++; } } } // Custom taxonomy support. if ( 1 === $position ) { $taxonomies = get_object_taxonomies( $post->post_type, 'objects' ); foreach ( $taxonomies as $tax ) { if ( ! $tax->hierarchical || 'category' === $tax->name ) { continue; } $terms = get_the_terms( $post_id, $tax->name ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { $items[] = array( '@type' => 'ListItem', 'position' => $position, 'name' => $terms[0]->name, 'item' => get_term_link( $terms[0] ), ); $position++; break; } } } } // Current page as the last item. $items[] = array( '@type' => 'ListItem', 'position' => $position, 'name' => $post->post_title, 'item' => get_permalink( $post_id ), ); // Override with AI-detected breadcrumbs if provided. if ( ! empty( $fields['itemListElement'] ) && is_array( $fields['itemListElement'] ) ) { $items = $this->format_field_items( $fields['itemListElement'] ); } $schema['itemListElement'] = $items; return $schema; } /** * Get the primary category for a post. * * Checks for Yoast primary category first, then falls back to the first one. * * @param int $post_id The post ID. * @param array $categories Array of category objects. * @return object|null The primary category or null. */ private function get_primary_category( $post_id, $categories ) { // Check for Yoast primary category. $primary_term_id = get_post_meta( $post_id, '_yoast_wpseo_primary_category', true ); if ( $primary_term_id ) { $primary = get_category( $primary_term_id ); if ( $primary && ! is_wp_error( $primary ) ) { return $primary; } } // Check for Rank Math primary category. $rank_math_primary = get_post_meta( $post_id, 'rank_math_primary_category', true ); if ( $rank_math_primary ) { $primary = get_category( $rank_math_primary ); if ( $primary && ! is_wp_error( $primary ) ) { return $primary; } } // Fall back to first category. return ! empty( $categories[0] ) ? $categories[0] : null; } /** * Get category hierarchy (from root to current). * * @param object $category The category object. * @return array Array of category objects from root to current. */ private function get_category_hierarchy( $category ) { $hierarchy = array( $category ); $parent_id = $category->parent; while ( $parent_id > 0 ) { $parent = get_category( $parent_id ); if ( $parent && ! is_wp_error( $parent ) ) { array_unshift( $hierarchy, $parent ); $parent_id = $parent->parent; } else { break; } } return $hierarchy; } /** * Format AI-provided breadcrumb items. * * @param array $items Raw item data. * @return array Formatted ListItem objects. */ private function format_field_items( $items ) { $formatted = array(); $position = 1; foreach ( $items as $item ) { if ( is_array( $item ) ) { $list_item = array( '@type' => 'ListItem', 'position' => $position, ); if ( isset( $item['name'] ) ) { $list_item['name'] = $item['name']; } if ( isset( $item['item'] ) ) { $list_item['item'] = $item['item']; } elseif ( isset( $item['url'] ) ) { $list_item['item'] = $item['url']; } $formatted[] = $list_item; $position++; } } return $formatted; } }