Create sub single pages

时间:2017-03-22 作者:William Ode

我想有可能创建“子单页”,以显示在自定义帖子类型的详细内容。是否有一种方法可以使用具有此类型URL的特定模板生成页面:

domain.com/custom-post-type-slug/single-slug/sub-single-slug/
注:我需要为我的文章(通过ACF添加的内容)的每个部分多个子单页。slug不需要是动态的,因为我的每个帖子的所有部分都是相同的。

非常感谢。

2 个回复
最合适的回答,由SO网友:William Ode 整理而成

以下是解决方案(来源: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.\'" />\';
}
别忘了冲洗你的永久性皮肤!转到“设置”>“永久链接”>“保存到刷新”

SO网友:Scott

首先,您需要注册自定义帖子类型,使其具有层次结构,即一篇帖子可以有一个父帖子。

之后,您需要确保将permalink结构设置为example.com/%postname%/.

一旦你有了它,你只需要创建一个名为sub-single-slug 和设置single-slug 作为WordPress后端编辑器的父级Page Attribute (确保已签入Screen Options). 仅此而已。现在您的sub-single-slug 帖子的链接结构如下example.com/custom-post-type/single-slug/sub-single-slug/.

例如,我注册自定义帖子类型如下:

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