akiyum
6/10/2015 - 10:52 AM

最上位のページのスラッグ取得

最上位のページのスラッグ取得

function ps_get_root_page( $cur_post, $cnt = 0 ) {
	if ( $cnt > 100 ) { return false; }
	$cnt++;
	if ( $cur_post->post_parent == 0 ) {
		$root_page = $cur_post;
	} else {
		$root_page = ps_get_root_page( get_post( $cur_post->post_parent ), $cnt );
	}
	return $root_page;
}

//親スラッグが引数ならTRUE それ以外ならFALSE
function check_parent_slug($parentslug){
	global $post;
	$parent_id = get_page_by_path($parentslug) -> ID;
	$crrent_id = ps_get_root_page($post) -> ID;
	if(is_page($parentslug)) return true;
	return ($parent_id === $crrent_id) ? ture : false;
}