1

Oddly enough I can't seem to find anyone asking this question exactly in this fashion.

Is it at possible to make a page be a child of a CPT? In a repeatable fashion (see below for more info) n.b. I do not mean vice versa -- which is easy enough see: Can a custom post type have a Parent Page?

Example:

Is that possible to achieve that permalink structure in a repeatable manner? That is I'd want to have multiple arbitrary children page post type posts underneath the arbitrary author post type posts?

N.b. not all author pages will need to have a /authors/author-name/thank-you some many need nothing at all, some may need a different slug e.g. /authors/jane-doe/happy-birthday-greeting

3

I can't really think of a way that you would have a post type of page and have it be "repeatable." As Jake said, it's going to involve rewrite rules. I think the easiest way to do it would be to set up some templates for whatever these cases may be in your theme directory, and make use of query vars to populate them. So, for example:

function wpse_add_query_vars( $vars ){
    $vars[] = "tmpl";
    $vars[] = "author_slug";
    return $vars;
}
add_filter( 'query_vars', 'wpse_add_query_vars' );

function wpse_add_rewrite_rule() {
    add_rewrite_rule( '^authors/([^/]*)/([^/]*)', 'index.php?author_slug=$matches[1]&tmpl=$matches[2]', 'top' );
}
add_action( 'init', 'wpse_add_rewrite_rule' );

function wpse_template_include( $template ) {
    if ( get_query_var( 'tmpl' ) && get_query_var( 'author_slug' ) ) {
        if ( file_exists( get_template_directory() . '/' . get_query_var( 'tmpl' ) . '.php' ) ) {
            $template = get_template_directory() . '/' . get_query_var( 'tmpl' ) . '.php';
        }
    }
    return $template;
}
add_filter( 'template_include', 'wpse_template_include' );

I tested this and it works, but it will probably need to be tweaked for your needs. Basically, here's what's happening:

  1. We register a tmpl query var so we can specify a template file slug as the name. We will have to create the templates we want in our theme folder.
  2. We'll add a rewrite rule so that if you hit /authors/author-name/template-name then we can load a template (if it exists). Make sure to flush your permalinks in settings after adding or changing any rewrite rules!
  3. Now we hook into template_include to see if we have the tmpl var set, check if we have a template in our theme directory, and return that template if we have it. So, for example, we hit /authors/author-name/thank-you, we will look for a thank-you.php template file in the theme folder and load that instead of a default template. You would have access to the author slug in the template file by using get_query_var().

This is just an abstract example and will obviously need to be tweaked for your needs, but using this method you would be able to make as many of these pages as you need and maintain them right from the theme. I also didn't create an author CPT to fully test this so you may need to adjust the rewrite rule so it doesn't conflict with other things you are trying to do with the author CPT (in fact, you may not even need to use the author_slug query var as it's probably available already from WP).

Here's some more resources:

  1. Custom Page Template Page Based on URL Rewrite
  2. The Rewrite API: Post Types & Taxonomies

Of course there's more than one way to skin a cat, but hope this helps point you in the right direction or at least gives you some ideas of what's possible.

1

I can't really think of an easy and repeatable way to do this, but check out: https://wordpress.org/support/topic/create-static-subpage-for-a-custom-post-type

You can create a custom parameter at the end of your post type. Once you get this you can load a template and use that parameter to query the page's content.

  • Please answer with more than a plain link. Links can go away, rendering your answer useless. Add the relevant parts to your question so it can help without following a link. – kaiser Jun 7 '16 at 8:33

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.