0

I have a rare scenario where I need an advanced Rewrite URLs in order to retrieve the desired URL structure. Here's the thing. I have 3 separate items to manipulate with:

  • Page
  • Custom post type
  • Taxonomy

I need the following URL structure

example.com/landing-page - This is the landing page
example.com/landing-page/child-page - This is the child of the landing page
example.com/landing-page/child-page/custom-post-type-name - This is the complicated part. 

So as you can see want I want to achieve is some kind of permalink hierarchy where the CPT uses both the landing and the child page in its slug. Is that possible at all?

Thanks!

  • Can you please share links or provide example code of things you're tried already and what the results were. – Andrew Jan 11 '18 at 15:45
0

You can do this in two ways: (1) with two Pages and a CPT with no archive, or (2) with one Page and a CPT with an archive.

Option 1 - two Pages:

Create your two regular Pages, with "child-page" set as a child page of "landing-page". When you register your CPT, set it to have no archive and set its rewrite so the individual CPTs are "under" "child-page".

register_post_type('wpse_post_type',
    array(
        'has_archive' => false,
        'rewrite' => array(
            'slug' => 'landing-page/child-page'
        ),
    )
);

This seems to answer your original question, with the benefit being that "child-page" is a Page in wp-admin so it's easy to edit its content. The downside is that unless you create a special page template - either page-child-page.php or tpl-child-page.php then chosen manually from the Page Template dropdown - you do not have any archive for your CPT.

Option 2 - one Page and CPT with archive

If "landing-page" is a Page, "child-page" is your CPT archive, and "custom-post-type-name" is a single CPT, you would set up "landing-page" as a normal Page.

When you register your CPT, to enable the "child-page" as the archive that lives one level higher than the individual CPTs:

register_post_type('wpse_post_type',
    array(
        'has_archive' => 'landing-page/child-page',
        'rewrite' => array(
            'slug' => 'landing-page/child-page'
        ),
    )
);

The benefit here is that child-page will use your existing archive.php so you may not have to adjust anything in your theme to have an archive of your new CPT. Of course, you can also customize this archive by creating an archive-wpse_post_type.php (change to your actual CPT name) in the theme. You can use custom fields to allow editing some of the content in wp-admin if you like.

One last tip - if you have already registered the CPT, be sure to use unregister_post_type() to wipe it out first, and then re-register it, because once a CPT is defined it's hard to change the rewrite rules without nuking it. (Don't worry if you have already created posts in the CPT. Unregistering it does not remove them from the database; as soon as you re-register the CPT, they will show up again, this time at the right URLs).

  • This is not enough because child-page may be something else. It's dynamic and I added there just as an example and mark. :) So I need this to work dynamically with all subpages of the landing-page. – Ivica Jan 11 '18 at 14:31

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.