1

I have an "About Us" page on my site with some child pages. I have created a "History" custom post type, where the rewrite slug is 'history'.

I use archive-cpt_history.php as the template file for that CPT. Everything works exactly as its configured, I can go to website.com/history/ to see my CPT loop in all its glory.

The issue is, I need this history CPT to be a child of About Us. How can I rewrite the slug to be /about-us/history so that it works with the archive / landing page? I can do this:

'rewrite' => array( 'slug' => '/about-us/history', 'with_front' => false ),

But that only works with the single posts, not the archive / landing page. Any help would be greatly appreciated.

Also, in case this is relevant, once I have solved this issue single-cpt_history.php will simply use " header( 'Location: ' . site_url('') ); " to redirect users back home. There is never any permalink to the individual History post. The design just doesn't work like that. The History landing page will always show all posts in the CPT.

1 Answer 1

3

In your register post type arguments, set rewrite to true and has_archive to the path you want for the archive page (without leading slash):

'rewrite' => true,
'has_archive' => 'about-us/history'

As for the redirection, rather than getting all the way to the template, you could short-circuit the loading of single posts earlier and remove the need for the single cpt template:

function wpa_parse_query( $query ){
    if( is_singular() && isset( $query->query_vars['cpt_history'] ) ){
        wp_redirect( home_url() );
    }
}
add_action( 'parse_query', 'wpa_parse_query' );
2
  • Thank you! Everything seems to be working without issue. Thanks for the added snippet for single post redirection too! Sadly I can't upvote you, because I need more rep points, but I've given you the correct answer.
    – beefchimi
    Commented Apr 23, 2013 at 18:43
  • Milo: Is it possible to add multiple parent pages? I have two sections: example.com/dev/workers and example.com/care/workers and I wanted workers to be a CPT with a category selection for the single posts… Commented May 14, 2013 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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