Check if post is a child

This snippet creates a conditional tag that check if a post is a child or not, and returns a value to use in an if-statement.

Code Snippet:

<?php
function ma_is_child_of($page_id) {
	global $post;
	$is_child = false;
	$parents = get_post_ancestors($post);
	if ($parents) {
		foreach ($parents as $one_parent_id) {
			if ($one_parent_id == $page_id) {
				$is_child = true;
				break;
			}
		}
	}
	return $is_child;
};
 ?>

Posted in Posts