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

Enfold Build a Parent Navigation Breadcrumb with Advanced Custom Fields

Enfold Build a Parent Navigation Breadcrumb with Advanced Custom Fields

<?php

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

function get_top_parent_page_id( $child_post_id = null ) {

	if( $child_post_id != null ) {

		$_post = get_post( $child_post_id );

	} else {
		global $post;
		$_post = $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' ) {

		$parent_page_id = '';

		if( $post->post_type == 'page' ) {
		    $parent_page_id = get_top_parent_page_id();
		} elseif ( $post->post_type == 'post' ) {
		    $parent_page_id = get_top_parent_page_id( get_field( 'acf_overview_page_news', 'option' ) );
		} else {
		    $parent_page_id = get_top_parent_page_id();
		}

		// Get all Child Pages
		$args_children = array(
			'post_status' 	=> 'publish',
			'post_type'		=> 'page',
			'post_parent' 	=> $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' 		=> $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' 		=> $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;
}

/*
 * Sets the correct "current page" CSS for the parent item for various post types
 * depending on their parent settings in the RED Indstilling
 */
add_action( 'wp_head', 'yanco_add_dynamic_navigation_styles' );
function yanco_add_dynamic_navigation_styles() {
	global $post;
	$parent_page_id = '';
	$color_grey_breadcrumbs = '#878787';

	switch( $post->post_type ) {
		case 'post':
			echo yanco_get_dynamic_navigation_styles( get_field( 'acf_overview_page_news', 'option' ), $color_grey_breadcrumbs );
			break;
		case 'reference':
			echo yanco_get_dynamic_navigation_styles( get_field( 'acf_overview_page_references', 'option' ), $color_grey_breadcrumbs );
			break;
		case 'ejendom':
			echo yanco_get_dynamic_navigation_styles( get_field( 'acf_overview_page_ejendomme', 'option' ), $color_grey_breadcrumbs );
			break;
		default:
			break;
	}

}

function yanco_get_dynamic_navigation_styles( $parent_page_id, $color_grey_breadcrumbs ) {
	$css = '';
	$css .= '<style>';
	$css .= 'li.page_item.page-item-' . $parent_page_id . ' > a { color: ' . $color_grey_breadcrumbs . ' !important; }';
	$css .= '</style>';
	return $css;
}