iamcanadian1973
4/5/2015 - 4:35 AM

Add current parent class to Bill Erickson's Sub Page Widget

Add current parent class to Bill Erickson's Sub Page Widget

<?php

//* Add current parent class to Bill Erickson's Sub Page Widget

add_filter('be_subpages_widget_class', 'be_add_class', 10, 2);

function be_add_class( $class, $subpage ) {
	
	global $post;
	
	$class[] = 'menu-' . sanitize_title( $subpage->post_title );
	
	if ( $subpage->ID == $post->ID )
			$class[] = 'current-menu-item';
			
	if( get_current_page_depth( $post->ID ) > 1 && $subpage->ID == $post->post_parent )
		$class[] = 'current-menu-ancestor';
	
	return $class;
}

/**
 * Get current page depth
 *
 * @return integer
 */
function get_current_page_depth( $page_id = '' ){
	global $wp_query;
	
	$object = $wp_query->get_queried_object();
	$parent_id  = $object->post_parent;
	$depth = 0;
	while($parent_id > 0){
		$page = get_page($parent_id);
		$parent_id = $page->post_parent;
		$depth++;
	}
 
 	return $depth;
}