If is this page or is child of it

A client of mine needed to display a certain HTML element (a social media icon) only on certain pages and children of those pages.
Put this in your functions.php file:

function ar_check_family( $page_id ) {
	global $post;
	if ( is_page() && ( $post->post_parent == $page_id || is_page( $page_id ) ) ) {
		return true;
	}
	else {
		return false;
	}
}

Put this in your template:

if ( ar_family_check( '2' ) ) { 
	echo 'This is a child of 2.';
	echo 'Display what you wanted to show here';
}

Leave a Comment





This site uses Akismet to reduce spam. Learn how your comment data is processed.