📜  自定义路由到具有动态参数 wordpress 的页面 - 任何代码示例

📅  最后修改于: 2022-03-11 14:55:21.052000             🧑  作者: Mango

代码示例1
function custom_rewrite_rule(){
    $page_id = 318; // replace this ID with the page with the iFrame template 

    $page_data = get_post($page_id);

    if(!is_object($page_data)){
        return; // all other pages don't have to support custom deeplinks
    }

    // catches deeplinks 1 level deep, i.e.: /members/profile
    add_rewrite_rule(
        $page_data->post_name . '/([^/]+)/?$',
        'index.php?pagename=' . $page_data->post_name . '&memberspage=$matches[1]',
        'top'
    );

    // catches deeplinks 2 levels deep, i.e.: /members/profile/edit
    add_rewrite_rule(
        $page_data->post_name . '/([^/]+)/([^/]+)/?$',
        'index.php?pagename=' . $page_data->post_name . '&memberspage=$matches[1]&members_param=$matches[2]',
        'top'
    );

    // catches 3 levels deep, i.e. /members/profile/edit/confirm
    add_rewrite_rule(
        $page_data->post_name . '/([^/]+)/([^/]+)/([^/]+)/?$',
        'index.php?pagename=' . $page_data->post_name . '&memberspage=$matches[1]&members_param=$matches[2]&members_param2=$matches[3]',
        'top'
    );

}

add_action('init', custom_rewrite_rule);