yan-k
9/22/2017 - 8:30 AM

List Navigation Tree for Pages

List Navigation Tree for Pages

<?php

/*
 * Get Ancenstor / Top Parent Page ID
 */

function get_top_parent_page_id() {
    global $post;
    $ancestors = $post->ancestors;

    // Check if page is a child page (any level)
    if ($ancestors) {
        //  Grab the ID of top-level page from the tree
        return end($ancestors);
    }

	// Page is the top level, so use  it's own id
	return $post->ID;
}

/*
 * List Navigation Tree for Pages
 */

function yanco_page_navigation() {
	global $avia_config;
	global $post;

	$header_settings = avia_header_setting();
	$html = '';

	if( $header_settings['header_title_bar'] == 'title_bar' ) {

		// Get all Child Pages
		$args_children = array(
			'post_status' 	=> 'publish',
			'post_type'		=> 'page',
			'post_parent' 	=> get_top_parent_page_id(),
			'orderby' 		=> 'title',
			'order'     	=> 'ASC',
		);

		$query_children = new WP_Query( $args_children );
		$posts_children = $query_children->posts;

		if( $post->ancestors ) {
			$args_parent = array(
				'post_status' 	=> 'publish',
				'post_type' 	=> 'page',
				'page_id' 		=> get_top_parent_page_id()
			);
			$query_parent = new WP_Query( $args_parent );
			$posts_parent = $query_parent->posts;
		} else {
			$args_parent = array(
				'post_status' 	=> 'publish',
				'post_type' 	=> 'page',
				'page_id' 		=> $post->ID,get_top_parent_page_id()
			);
			$query_parent = new WP_Query( $args_parent );
			$posts_parent = $query_parent->posts;
		}

		$_posts = array_merge( $posts_parent, $posts_children );
		$_posts_ids = array();

		foreach ($_posts as $key => $value) {
			array_push( $_posts_ids, $value->ID );
		}

		$args_list_pages = array(
			'echo'		=> false,
			'include'	=> $_posts_ids,
			'title_li'	=> '',
		);

		$html .= '<ul>';
		$html .= wp_list_pages( $args_list_pages );
		$html .= '</ul>';

	}

	return $html;
}