I have two custom post types - 'gaming-consoles' and 'xbox-games'.

I also have two custom templates for these post types: single-gaming-consoles.php and single-xbox-games.php - which worked great.

I ran into a problem due I wanted the following URL-stucture on my website:

website.com/gaming-consoles/xbox/             <--- Custom Post Type 'gaming-consoles' 
website.com/gaming-consoles/xbox/xbox-games/  <--- Custom Post Type 'xbox-games'

Custom post types can't have other types of custom post types as a parent.

So I used the following solution made by Janina to make it work, and solved it.

add_action('admin_menu', function() { remove_meta_box('pageparentdiv', 'xbox-games', 'normal');});
add_action('add_meta_boxes', function() { add_meta_box('chapter-parent', 'gaming-consoles', 'chapter_attributes_meta_box', 'xbox-games', 'side', 'high');});
function chapter_attributes_meta_box($post) {
    $post_type_object = get_post_type_object($post->post_type);
        if ( $post_type_object->hierarchical ) {
            $pages = wp_dropdown_pages(array('post_type' => 'gaming-consoles', 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)'), 'sort_column'=> 'menu_order, post_title', 'echo' => 0));
        if ( ! empty($pages) ) {
        echo $pages;
        } // end empty pages check
    } // end hierarchical check.

I also used this solution by Pablo Vallejo to remove the Slug /gaming-consoles/ - so the URLs becomes just the following:

website.com/xbox/                            <--- Custom Post Type 'gaming-consoles' 
website.com/xbox/xbox-games/                 <--- Custom Post Type 'xbox-games'

Now, the custom page of website.com/xbox/ is working but website.com/xbox/xbox-games/ is not. I am getting the index.php instead of the page content.

The single-xbox-games.php starts with:

     Template Name: Xbox-games Template

As I said, it worked before I added the parent functionality.

According to the Template Hierarchy it seems that Wordpress is looking for another name than single-xbox-games.php.

Anyone got an idea how to display the template for 'xbox-games'?

Your Answer

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

Browse other questions tagged or ask your own question.