How to load an element for a whole “branch” of content in WordPress

It seems that there isn’t currently a WordPress core function that allows to test if a page belongs to a specific tree of pages.
For example, you might want to load a special element for a whole “branch” of content. Like a “services” page and all its children single pages. It is a pretty common need, actually.

Add this code to functions.php:

and call it into templates with this