0

A site I'm currently working on has a page structure like this

About Us
|
|_People
| |
|  _ Person 1
|  _ Person 2
|  _ Person 3
|... etc ...

Every 'Person' page is a separate page, but they all have the same structure using a few ACF fields to display a person's bio, photo etc.

I know how to create a custom page template for one specific page, i.e. page-slug.php, but I want to use one page template for all of those subpages.

What would be the easiest way to accomplish this?

4

You can always make use of the page_template filter to tell WordPress to use a specific page template for all child pages of a certain parent. It is as easy as creating a special page template, lets call it page-wpse-person.php.

Now it is as easy as including that template whenever a child page of people is being viewed. For the sake of example, lets say the page ID of people is 10

add_filter( 'page_template', function ( $template ) use ( &$post )
{
    // Check if we have page which is a child of people, ID 10
    if ( 10 !== $post->post_parent )
        return $template;

    // This is a person page, child of people, try to locate our custom page
    $locate_template = locate_template( 'page-wpse-person.php' );

    // Check if our template was found, if not, bail
    if ( !$locate_template )
        return $template;

    return $locate_template;
});
|improve this answer|||||
  • Thanks Pieter. This was exactly what I was looking for and just set it up this way. – Ruben Jul 6 '16 at 20:03
  • My pleasure, glad it worked for you. Enjoy ;-) – Pieter Goosen Jul 7 '16 at 4:23
3

The easiest way would be to create a custom post type 'person', in which you include the custom fields. The WordPress template hierarchy would then ensure that the single-person.php template is used for those posts.

|improve this answer|||||
  • Thanks for the suggestion, but I prefer to stick to pages so in the admin panel I keep that same visual page structure which is easier to use for the editor. I went with Pieter's solution. – Ruben Jul 6 '16 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.