Create sub single pages

All we need is an easy explanation of the problem, so here it is.

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!

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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' );

Method 2

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.'" rel="nofollow noreferrer noopener" />';
}

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

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply