X

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;
};
 ?>

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!