2

I would like to have the possibility to create "sub single pages" to display detailed content in a custom post type. Is there a way to generate pages, with a specific template with a URL of this type:

domain.com/custom-post-type-slug/single-slug/sub-single-slug/

Note : I need multiple sub-single pages for each section of my post (content added via ACF). The slug doesn't need to be dynamic because all of my sections are the same for each post.

Thank you!

1

3 Answers 3

2

First, you need to register the custom post type so that it has hierarchy, i.e. a post can have a parent post.

After that, you need to make sure your permalink structure is set to example.com/%postname%/.

Once you have that, you only need to create a child custom post named sub-single-slug and set single-slug as its parent from WordPress backend Editor's Page Attribute (make sure it's checked in Screen Options). That's all. Now your sub-single-slug post will have the link structure as example.com/custom-post-type/single-slug/sub-single-slug/.

For example, I register the custom post type as follows:

function wpse_register_custom_post_type() {

    $labels = array(
        "name" => __( 'custom post type', 'text-domain' ),
        "singular_name" => __( 'custom post types', 'text-domain' ),
    );

    $args = array(
        "label" => __( 'custom post type', 'text-domain' ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_menu" => true,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => true,
        "rewrite" => array( "slug" => "custom_post_type", "with_front" => true ),
        "query_var" => true,
        "supports" => array( "title", "editor", "thumbnail", "custom-fields", "page-attributes" ),
        "taxonomies" => array( "category", "post_tag" ),
    );

    register_post_type( "custom_post_type", $args );
}

add_action( 'init', 'wpse_register_custom_post_type' );
1
  • Very interesting approach! I did not know that a Custom Post Type could be hierarchical! Nevertheless, this method requires creating a sub-post, which will be complex for the contributor. In my idea, I would like to enter the content in the parent-post (with ACF tabs: parent-page, sub-page-1, sub-page-2 ...) and then dispatch it in sub-templates for Each of the sub-posts. In fact, my question is badly asked: how to generate, for example, 5 virtual sub-posts (and for each a separate template) for each post of a given custom post type? Thank you for your reply Commented Mar 23, 2017 at 8:11
1

Here is the solution (origin : http://www.placementedge.com/blog/create-post-sub-pages/)

// Fake pages' permalinks and titles. Change these to your required sub pages.
$my_fake_pages = array(
    'reviews' => 'Reviews',
    'purchase' => 'Purchase',
    'author' => 'Author Bio'
);

add_filter('rewrite_rules_array', 'fsp_insertrules');
add_filter('query_vars', 'fsp_insertqv');

// Adding fake pages' rewrite rules
function wpse_261271_insertrules($rules)
{
    global $my_fake_pages;

    $newrules = array();
    foreach ($my_fake_pages as $slug => $title)
        $newrules['books/([^/]+)/' . $slug . '/?$'] = 'index.php?books=$matches[1]&fpage=' . $slug;

    return $newrules + $rules;
}

// Tell WordPress to accept our custom query variable
function wpse_261271_insertqv($vars)
{
    array_push($vars, 'fpage');
    return $vars;
}

// Remove WordPress's default canonical handling function

remove_filter('wp_head', 'rel_canonical');
add_filter('wp_head', 'fsp_rel_canonical');
function wpse_261271_rel_canonical()
{
    global $current_fp, $wp_the_query;

    if (!is_singular())
        return;

    if (!$id = $wp_the_query->get_queried_object_id())
        return;

    $link = trailingslashit(get_permalink($id));

    // Make sure fake pages' permalinks are canonical
    if (!empty($current_fp))
        $link .= user_trailingslashit($current_fp);

    echo '<link rel="canonical" href="'.$link.'" />';
}

DO NOT forget to flush your permalinks! Go to Settings > Permalinks > Save to flush

1
  • Please don't just add a link only answer. What happens if the link is removed in future? So SE policy is you should add the key part from the link within the answer.
    – Scott
    Commented Mar 26, 2017 at 4:20
0

Yes, it is possible to achieve the desired URL structure and create sub-single pages for your custom post type. To accomplish this, you can use the WordPress rewrite rules and template hierarchy.

Here are the steps you can follow:

  1. Register the Custom Post Type: Assuming you have already registered your custom post type, make sure you have set the 'hierarchical' argument to true when registering the post type. This allows you to create a hierarchical structure for your posts.

  2. Create a Custom Rewrite Rule: You need to create a custom rewrite rule to handle the sub-single pages. Add the following code to your theme's functions.php file or a custom plugin:

    function custom_post_type_rewrite_rules() {
        add_rewrite_rule(
            '^custom-post-type-slug/([^/]+)/([^/]+)/?$',
            'index.php?post_type=custom-post-type-slug&name=$matches[2]&sub_single=$matches[1]',
            'top'
        );
    }
    add_action('init', 'custom_post_type_rewrite_rules');
    

This code adds a rewrite rule that captures the URL structure you mentioned and maps it to the custom post type's single page.

  1. Flush Rewrite Rules: After adding the custom rewrite rule, you need to flush the rewrite rules for it to take effect. You can do this by simply visiting the "Settings > Permalinks" page in your WordPress admin area and clicking the "Save Changes" button. This will flush the rewrite rules.

  2. Create Custom Templates: To display the content of the sub-single pages with a specific template, you can create a custom template file in your theme. Create a new file named single-custom-post-type-slug.php in your theme's directory. This template will be used for the main single page of your custom post type.

Inside the single-custom-post-type-slug.php file, you can add code to check for the sub_single query variable and load different content based on its value. Here's an example:

    <?php
    get_header();
    
    if (have_posts()) {
        while (have_posts()) {
            the_post();
    
            $sub_single = get_query_var('sub_single');
    
            if (empty($sub_single)) {
                // Display the main content of the post
                the_content();
            } else {
                // Display the content of the sub-single page
                // You can use the $sub_single variable to determine which section to display
                // For example: if ($sub_single === 'section-1') { ... }
            }
        }
    }
    
    get_footer();
    ?>

Make sure to customize the code within the if-else statement to display the desired content for each sub-single page section.

  1. Create Sub-Single Pages: Now you can create sub-single pages within your custom post type. For example, if your custom post type is named "Books," you can create a new book post and set the slug to "my-book." Then you can create sub-single pages with URLs like:

    • domain.com/custom-post-type-slug/my-book/section-1/
    • domain.com/custom-post-type-slug/my-book/section-2/
    • domain.com/custom-post-type-slug/my-book/section-3/ and so on.

    These URLs will be mapped to the custom template file single-custom-post-type-slug.php, and you can customize the content for each section within that template.

That's it! You should now have the ability to create sub-single pages with the specified URL structure and display detailed content for each section of your custom post type.

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.