规则出现问题,因为给定projecttype
在您访问相关URL时添加。
每次您访问projects/{projectstype}/
地址和刷新重写规则,they are deleted and only one rule (仅适用于当前项目类型)已保存/创建。
您可以将代码添加到request
过滤器挂钩,则无需添加重写规则。由规则设置的所有查询变量(仅限您设置page_id
), 您可以手动设置。
add_filter( \'request\' , \'se336968_request\' );
function se336968_request( $query_vars )
{
if(substr( $_SERVER[\'REQUEST_URI\'], 0, 11 ) === \'/projects/\'){
$request_subpage = str_replace(\'/\', \'\', str_replace(\'/projects/\', \'\', $_SERVER[\'REQUEST_URI\']));
if($request_subpage !== \'\') {
$project_types = get_posts( array( \'post_type\' => \'projecttype\', \'posts_per_page\' => -1 ) );
foreach( $project_types as $project_type ) {
if( $project_type->post_name === $request_subpage ) {
// your current rewrite
$query_vars[\'page_id\'] = 8;
$query_vars[\'post_type\'] = \'page\';
// --- to open "projecttype" type post from URL ---
// $query_vars[\'post_type\'] = \'projecttype\';
// $query_vars[\'projecttype\'] = $request_subpage;
// unset( $query_vars[\'page_id\'], $query_vars[\'p\'], $query_vars[\'pagename\'], $query_vars[\'name\'] );
break;
}
}
}
}
return $query_vars;
}