wordpress - Wordpress 重写规则 - 无法获得所需的确切结构
问题描述
我有一个房地产 wordpress 网站,我们有一个需要遵循的自定义 url 结构,如下所示:
domain.com/status/country/city/area/type/propertyname
现在这是可行的,但是如果我们想显示一些链接,例如:
domain.com/status/country/city/type
或者
domain.com/status/country/type
这将不起作用,因为结构由于某种原因无法读取额外的规则。主要结构,当您删除一个参数时,它应该显示结果,但是如果您删除一个参数并添加“类型”,它将不会读取它,我在下面发布代码:
<?php
// code will goes here
// status/country/city/area/type
function add_hierarchy_rewrite_rules5() {
add_rewrite_rule(
'([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$',
'index.php?property_status=$matches[1]&property_country=$matches[2]&property_city=$matches[3]&property_area=$matches[4]&property_type=$matches[5]',
'top'
);
add_rewrite_tag('%property_statusy%','([^/]*)');
add_rewrite_tag('%property_country%','([^/]*)');
add_rewrite_tag('%property_city%','([^/]*)');
add_rewrite_tag('%property_area%','([^/]*)');
add_rewrite_tag('%property_type%','([^/]*)');
add_filter( 'wp_title', 'set_custom_title', 10 );
}
add_action('init', 'add_hierarchy_rewrite_rules5');
// status/country/city/area/type/2
function add_hierarchy_rewrite_rules5_pagination() {
add_rewrite_rule(
'([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([0-9]+)/?$',
'index.php?property_status=$matches[1]&property_country=$matches[2]&property_city=$matches[3]&property_area=$matches[4]&property_type=$matches[5]&page=matched[6]',
'top'
);
add_rewrite_tag('%property_statusy%','([^/]*)');
add_rewrite_tag('%property_country%','([^/]*)');
add_rewrite_tag('%property_city%','([^/]*)');
add_rewrite_tag('%property_area%','([^/]*)');
add_rewrite_tag('%property_type%','([^/]*)');
}
add_action('init', 'add_hierarchy_rewrite_rules5_pagination');
// status/country/city/type
function add_hierarchy_rewrite_rules4() {
add_rewrite_rule(
'([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$',
'index.php?property_status=$matches[1]&property_country=$matches[2]&property_city=$matches[3]&property_type=$matches[4]',
'top'
);
add_rewrite_tag('%property_status%','([^/]*)');
add_rewrite_tag('%property_country%','([^/]*)');
add_rewrite_tag('%property_city%','([^/]*)');
add_rewrite_tag('%property_type%','([^/]*)');
}
add_action('init', 'add_hierarchy_rewrite_rules4');
// status/country/city/type/2
function add_hierarchy_rewrite_rules4_pagination() {
add_rewrite_rule(
'([^/]+)/([^/]+)/([^/]+)/([^/]+)/([0-9]+)/?$',
'index.php?property_status=$matches[1]&property_country=$matches[2]&property_city=$matches[3]&property_type=$matches[4]&page=$matches[5]',
'top'
);
add_rewrite_tag('%property_status%','([^/]*)');
add_rewrite_tag('%property_country%','([^/]*)');
add_rewrite_tag('%property_city%','([^/]*)');
add_rewrite_tag('%property_type%','([^/]*)');
}
add_action('init', 'add_hierarchy_rewrite_rules4_pagination');
// status/country/type
function add_hierarchy_rewrite_rules3() {
add_rewrite_rule(
'([^/]+)/([^/]+)/([^/]+)/?$',
'index.php?property_status=$matches[1]&property_country=$matches[2]&property_type=$matches[3]',
'top'
);
add_rewrite_tag('%property_status%','([^/]*)');
add_rewrite_tag('%property_country%','([^/]*)');
add_rewrite_tag('%property_type%','([^/]*)');
}
add_action('init', 'add_hierarchy_rewrite_rules3');
// status/country/type/2
function add_hierarchy_rewrite_rules3_pagination() {
add_rewrite_rule(
'([^/]+)/([^/]+)/([^/]+)/([0-9])+/?$',
'index.php?property_status=$matches[1]&property_country=$matches[2]&property_type=$matches[3]&page=$matches[4]',
'top'
);
add_rewrite_tag('%property_status%','([^/]*)');
add_rewrite_tag('%property_country%','([^/]*)');
add_rewrite_tag('%property_type%','([^/]*)');
}
add_action('init', 'add_hierarchy_rewrite_rules3_pagination');
// status/type
function add_hierarchy_rewrite_rules2() {
add_rewrite_rule(
'([^/]+)/([^/]+)/?$',
'index.php?property_status=$matches[1]&property_type=$matches[2]',
'top'
);
add_rewrite_tag('%property_status%','([^/]*)');
add_rewrite_tag('%property_type%','([^/]*)');
}
add_action('init', 'add_hierarchy_rewrite_rules2');
// status/type/2
function add_hierarchy_rewrite_rules2_pagination() {
add_rewrite_rule(
'([^/]+)/([^/]+)/([0-9]+)/?$',
'index.php?property_status=$matches[1]&property_type=$matches[2]&page=$matches[3]',
'top'
);
add_rewrite_tag('%property_status%','([^/]*)');
add_rewrite_tag('%property_type%','([^/]*)');
}
add_action('init', 'add_hierarchy_rewrite_rules2_pagination');
// status
function add_hierarchy_rewrite_rules1() {
add_rewrite_rule(
'([^/]+)/?$',
'index.php?property_status=$matches[1]',
'top'
);
add_rewrite_tag('%property_status%','([^/]*)');
}
add_action('init', 'add_hierarchy_rewrite_rules1');
// status/2
function add_hierarchy_rewrite_rules1_pagination() {
add_rewrite_rule(
'([^/]+)/([1-9]*)/?$',
'index.php?property_status=$matches[1]&page=[matched[2]]',
'top'
);
add_rewrite_tag('%property_status%','([^/]*)');
}
add_action('init', 'add_hierarchy_rewrite_rules1_pagination');
function set_custom_title($title) {
$status = get_query_var('property_status');
$country = get_query_var('property_country');
$city = get_query_var('property_city');
$area = get_query_var('property_area');
$type = get_query_var('property_type');
$new_title = "$status $country $city $area $type";
return $new_title;
}
add_filter( 'the_title', 'set_custom_title' );
// property-cyprus/city/limassol/commercial-investment/page/2
//function add_hierarchy_paging_rewrite_rules() {
// add_rewrite_rule(
// 'property-cyprus/city/([^/]+)?/([^/]+)/page/([1-9]+)/?$',
// 'index.php?property-city=$matches[1]&property-type=$matches[2]&page=$matches[3]',
// 'top'
// );
//}
//add_action('init', 'add_hierarchy_paging_rewrite_rules');
?>
这是我们想要的结构,相关属性应该显示在每个 url 下:
- domain.com/status/country/city/area/type/propertyname
- domain.com/status/country/city/area/type/
- domain.com/status/country/city/area/
- domain.com/status/country/city/
- domain.com/status/country/
- domain.com/status/
- domain.com/status/type
- domain.com/status/country/type
- domain.com/status/country/city/type
这可能吗?任何帮助将不胜感激。
解决方案
推荐阅读
- apache-kafka - Flink 停止静默消费 Kafka 主题
- laravel - Vue SPA 中的角色和权限
- ios - 在 Combine 中将一个发布者转换为另一个发布者
- html - 如何使用 Ruby 在控制台中输出带有 furiga 的汉字?
- apollo-client - 使用订阅时无法在上下文中找到“客户端”或作为选项问题传入
- javascript - 在前端显示 CSS 变量的值
- ios - swift AVAudioEngine 故障
- java - 使用 JavaParser 重构一个 java 文件
- r - R:如果 randomForest() 生成 boostrapped 常量会发生什么?
- ms-access - 如何使用另一列的排序顺序对条形图进行排序?