Walker::get_number_of_root_elements( array $elements )

Calculates the total number of root elements.

Parameters

$elements

(array) (Required) Elements to list.

Return

(int) Number of root elements.

More Information

Counts the number of top-level items (no children or descendants) in the provided array, and returns that count.

Source

File: wp-includes/class-wp-walker.php

public function get_number_of_root_elements( $elements ) {
		$num          = 0;
		$parent_field = $this->db_fields['parent'];

		foreach ( $elements as $e ) {
			if ( empty( $e->$parent_field ) ) {
				$num++;
			}
		}
		return $num;
	}

Changelog

Version Description
2.7.0 Introduced.

© 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/walker/get_number_of_root_elements