questions

WordPress custom post types with child pages

Rate this post

I am having difficulty trying to find a solution for my problematic.

We have a custom post type in wordpress named “stores” each store has a unique name example “LA store, NY Store” and each store will have child pages like location, opening hours etc.

But unfortunately its a hard work to create all child pages for each store. instead we would like to use some templates for the child pages.

We would like to have our url structure like:

sample.com/store/lastore/opening-hours

 

✔️Solution:

For this, you can modify the rewrite parameter when you register your post type. Below is an example:

$args = array(
           'labels'              => $labels,
           'hierarchical'        => true, // just to get parent child relationships with your custom post [but you already knew this part]
           'description'         => 'Your custom post type description here',
           'has_archive'         => false,
           'rewrite'             => array('slug' => '/'), // we rewrite the slug to the root url so that it has no post type prefix
           'with_front'          => false, // This is optional but just in case your posts are set to /blog/post-name this will make it /post-name for your custom post type 
           'capability_type'     => 'post',
           'supports'            => array(
                                          'title', 'editor', 'author', 'thumbnail', 'page-attributes'
                                    )
        );

register_post_type( 'post_type', $args );

Make sure you update the permalink structure after changes, so WordPress can rewrite the rules.

Leave a Reply

Your email address will not be published.

Back to top button