Search for
Login | Username Password Forgot? | Email: | Create Account
Technology / Internet | Popularity: 0 | Entries: 4 | Modified: 1515d 14h ago | | Add to My Feeds

Ever need to identify sections of your wordpress blog? Want your child pages of a particular parent to look or behave a specific way? Maybe each section should have it’s name at the top, or a specific header image. It can also be used for changing the navigation structure based on which section the user is in.  Heres a function that will return the post ID of the top-most page or section. You can define this anywhere, but usually works best in your theme’s functions.php file.

function get_first_ancestor($this_post_id, $section=0){
	// this takes 2 parameters: a post ID (usually $post->ID) and a section (can be left blank)
	$ancestor = get_post($this_post_id)->ID;
	while($ancestor != $section){
		$newpost = get_post($ancestor);
		$the_ancestor_id = $newpost->ID;
		$ancestor = $newpost->post_parent;
	}
	return $the_ancestor_id;
}

Usage example: This will return the ID of the top level parent

$ancestoryID = get_first_ancestor($post->ID);
wp_list_pages("title_li=&child_of=".$ancestoryID);

[ Report Post ]


More from Kernal OOP


^ Back To Top