首页 > 解决方案 > 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 下:

  1. domain.com/status/country/city/area/type/propertyname
  2. domain.com/status/country/city/area/type/
  3. domain.com/status/country/city/area/
  4. domain.com/status/country/city/
  5. domain.com/status/country/
  6. domain.com/status/
  7. domain.com/status/type
  8. domain.com/status/country/type
  9. domain.com/status/country/city/type

这可能吗?任何帮助将不胜感激。

标签: wordpressurl-rewriting

解决方案


推荐阅读