WordPress: Checking The Post Parent

If you are working with pages in WordPress that have sub-pages, there is a chance that at some point you are going to need to add conditions that apply to these sub-pages.

This is pretty easy and can be done by putting the following code in the relevant template file:

	if ($post->post_parent == "123") {
		// If the page parent ID is 123, do stuff here...

Now, if you have sub-pages that have more sub-pages and you want a condition to apply to all pages under the “grandparent” page, simply use the following code:

	$ancestors = get_post_ancestors($post);
	if (in_array(123, $ancestors)) {
		// If the page grandparent ID is 123, do stuff here...
Free Graphics, Icons, Tutorials
Free Graphics Free Christmas Vector Icon Graphics Pack 2017Free Fitness Vector Icons Graphics PackFree Camping Vector Graphics PackFree Summer Graphics PackFree File Icon PackFree Fast Food Vector Graphics
Sharing is caring...
Like & Follow
Share the love, share this page! Facebook Twitter Digg Reddit LinkedIn Pinterest Email
Close [X]
The Web Taylor
1000 Lakeside North Harbour Portsmouth, Hampshire PO6 3EN
02392 123358