During a recent website revision, a client mentioned that they needed a disclaimer on every one of their product pages. Normally, if the product pages were all set to a certain template, it’d be easy enough to just add that disclaimer into the template. Unfortunately, there was no need to make those pages a special template, so they were all just the default template.
However, all of the pages were descendants of the same great-grandparent, the catalog. I needed to find a way to check if a page was a grandchild of the catalog page, and if so, display the disclaimer.
Here’s the very simple code that made that happen:
<?php $ancestors = get_post_ancestors($post); if (in_array(4,$ancestors)) echo "4 is an ancestor of the post"; ?>
The code gets all the ancestors of the post, throws them into an array, and then checks to see if the catalog page (in this case, page ID 4) is in the array). If that’s true, we can go ahead and display the information we need to.
Tags: wordpress




Ha, and here I thought this was going to have something to do with actual Grandparents.
“If the user’s IP address matches this list of IP addresses of known grandparents, show a special welcome message header and bonus photos of the grandkids.”
Haha! That’s actually a great idea, for Maddox’s site. I’ll have to remember that.
Short and elegant. This was a HUGE help, thank you very much!