Return to Snippet

Revision: 18801
at October 8, 2009 15:25 by cyberhobo


Initial Code
function is_child_of( $page_id, $potential_child_id = '' ) {

	$is_child = false;

	if ( ! is_int( $page_id ) ) {
		$page = get_page_by_path( $page_id );
		$page_id = empty( $page ) ? 0 : $page->ID;
	}

	if ( empty( $potential_child_id ) ) { 
		$potential_child_id = get_the_ID();
	}

	$potential_child = get_page( $potential_child_id );

	if ( ! empty( $potential_child ) && is_array( $potential_child->ancestors ) ) {
		$is_child = in_array( $page_id, $potential_child->ancestors );
	}

	return $is_child;
}

Initial URL


Initial Description
This makes template code that applies to children of a particular page much more readable, especially if you can use a page path instead of an ID: `if ( is_child_of( 'topic/subtopic' ) ) :`.

Initial Title
WordPress is_child_of: Is this a child of page x?

Initial Tags
template, wordpress, function

Initial Language
PHP