Wordpress 在单个url中组合多个自定义用户分类

Wordpress 在单个url中组合多个自定义用户分类,wordpress,custom-taxonomy,Wordpress,Custom Taxonomy,我已经为用户创建了自定义用户分类法,它对单一分类法很有用 在下面的参考指南中,我创建了自定义用户分类法 参考指南:- 下面是分类学的结构 (registered taxonomy) profession Dietitian (under the profession ) (registered taxonomy) city NewYork(under the city) 我的

我已经为用户创建了自定义用户分类法,它对单一分类法很有用

在下面的参考指南中,我创建了自定义用户分类法

参考指南:-

下面是分类学的结构

(registered taxonomy)  profession

                              Dietitian (under the profession )



(registered taxonomy) city
                         NewYork(under the city)
我的鼻涕虫名字
expert

在这里,我想显示过滤结果,其中用户是
纽约市的
营养师
。因此,它将在配置文件中显示所有选择了上述选项的用户

现在我希望url类似于
www.test.com/expert/dieterian/newyork
在用户配置文件中选择纽约营养师的用户。纽约的营养师有什么解决方案吗


带有单个术语的url的工作方式如下:-
www.test.com/expert/dietitian/

此重写规则应该有效(假设“profession”和“city”是分类法注册名称):

代码:

function custom_rewrite_rules() {
    add_rewrite_rule('^profession/(.*)/city/(.*)?', 'index.php?profession=$matches[1]&city=$matches[2]', 'top');
}
add_action('init', 'custom_rewrite_rules');
在站点中保存此代码后,请记住刷新重新布线规则

网址:

我假设您已经在的教程上花费了大量时间,因此我将只讨论多分类筛选和前端显示所需的部分

在阅读长篇课文之前,你可以先尝尝

所以,第一件事是第一件事。 单分类法和多分类法的逻辑基础是相同的。 在注册分类法及其术语并完成wp管理所需的所有工作后,我们需要:

A)访问者可以在其中选择分类查询的页面(我们称之为TAX-QUERY页面)

B)另一个页面(我们称之为税务结果页面),其中将显示税务查询页面的结果

让我们潜水吧


单一分类法 税务查询页面

在完成本教程之后,显示分类法链接的页面的原始形式应如下所示:

访问者只需单击提供的链接即可“选择”他们想要查看的分类法

注意:

function custom_rewrite_rules() {
    add_rewrite_rule('^profession/(.*)/city/(.*)?', 'index.php?profession=$matches[1]&city=$matches[2]', 'top');
}
add_action('init', 'custom_rewrite_rules');
A)以下链接:

  • 提供的服务将类似于: www.example.com/user/profession/developer/
  • 城市将是这样的: www.example.com/user/city/gotham/
  • B)slug“user/city”和“user/profession”在函数中定义:my\u register\u user\u taxonomy>>register\u taxonomy>>rewrite>>slug

    税务结果页面

    单击上面提到的链接将进入一个页面(当然需要由您定义),该页面显示相同分类术语下的所有用户

    这是通过创建自定义分类模板来实现的

    您可以创建taxonomy-profession-developer.php文件来处理单个分类法和术语,或者创建taxonomy-profession.php来处理单个分类法及其所有术语,或者创建taxonomy.php来处理所有分类法及其术语

    在我看来,如果您不希望服务器上充斥着由您手动创建的模板文件,那么您应该使用taxonomy.php并为所有分类法创建一个通用模板,该模板描述了您希望自动显示的结果

    注意:

    function custom_rewrite_rules() {
        add_rewrite_rule('^profession/(.*)/city/(.*)?', 'index.php?profession=$matches[1]&city=$matches[2]', 'top');
    }
    add_action('init', 'custom_rewrite_rules');
    
    A)如本教程所述,要通过用户进行筛选并仅获取所需分类术语下的用户,需要自定义查询(在taxonomy.php文件中)

    B)Permalinks应该位于wp admin>>settings>>PermalinksPost Name选项上,这样wordpress就可以找到你的分类法,获取文件taxonomy.php,并提供有关可在taxonomy.php文件中使用的所选分类法的有用信息


    多重分类法 税务查询页面

    我们需要创建一个页面,让访问者从自定义分类中选择术语。此页面将通过链接(如get variables**)向TAX-RESULTS页面(taxonomy.php)提供所需的术语,以便进行查询

    **为了在TAX-RESULTS页面中拥有漂亮的永久链接,我们需要在function.php中添加以下重写函数(找到它),并在wp admin中刷新永久链接:

    function eg_add_rewrite_rules() {
      global $wp_rewrite;
      $new_rules = array(
          'user/(profession|city)/(.+?)/(profession|city)/(.+?)/?$' => 'index.php?post_type=eg_event&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2) . '&' . $wp_rewrite->preg_index(3) . '=' . $wp_rewrite->preg_index(4),
          'user/(profession|city)/(.+)/?$' => 'index.php?post_type=eg_event&' . $wp_rewrite->preg_index(1) . '=' . $wp_rewrite->preg_index(2)
      );
      $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }
    add_action( 'generate_rewrite_rules', 'eg_add_rewrite_rules' );
    
    请注意

    如果注册更多分类法,函数中的“职业|城市”应变为“职业|城市| tax1 | tax2 | tax3”

    因此,税务查询页面将是:

    HTML

    <div id="taxonomies">
        <h1>Find your professional</h1>
        <form>
            <?php if ( $taxonomies = get_object_taxonomies( 'user' ) ) : //get all taxonomies under the object_type "user" ( The second parameter given to the function my_register_user_taxonomy of the tutorial )
                foreach ( $taxonomies as $taxonomy ) : ?>
                    <p>
                        <ul>
                            <fieldset id="<?php echo esc_attr( $taxonomy ); ?>"> <!-- group the form by taxonomies -->
                                <legend><h2>Choose <?php echo $taxonomy; ?></h2></legend>
                                <?php if ( $terms = get_terms( $taxonomy ) ) : //get taxonomy's terms
                                    foreach ( $terms as $term ) : ?>
                                        <li><input type="checkbox" value="<?php echo esc_attr( $term -> slug ); ?>"> <?php echo $term -> name; ?></li>
                                    <?php   endforeach;
                                endif; ?>
                            </fieldset>
                        </ul>
                    </p>
                <?php   endforeach;
            endif; ?>
        </form>
        <a id="multiTaxSubmit" href="<?php echo esc_attr( get_home_url() . '/user' ); ?>">SUBMIT</a> <!-- this link is processed by jQuery to provide the taxonomy.php with the proper values. The href attribute is the base url needed by the taxonomy.php -->
    </div>
    
    税务结果页面(taxonomy.php)

    
    /*展示*/
    职业:
    城市:
    我们很抱歉。没有结果符合您的标准。
    请重新搜索!
    
    要刷新主题或插件中的永久链接或重写规则,需要使用
    flush\u rewrite\u rules()
    函数

    <?php 
    function custom_rewrite_rules() {
        flush_rewrite_rules();
        add_rewrite_rule( '^products/([^/]*)/([^/]*)/(\d*)?',
        'index.php?product_type=$matches[1]&product_brand=$matches[2]&p=$matches[3]',
        'top' );
            //Post Type: products
            //Taxonomy: product_type
            //Taxonomy: product_brand
    }
    add_action('init', 'custom_rewrite_rules');
    ?>
    
    
    
    这篇来自网络网站的帖子是否有帮助:“?@MirosławZalewski我需要它作为用户分类类型。你有什么棘手的想法吗???用户分类类型是什么意思?你是说post类型吗?@nvartolomei用于此类型,因为你的演示链接看起来是正确的答案,但我需要一些时间在站点上实现,现在正忙于其他任务。@SanjayNakate运气好吗?我没有尝试忙于其他任务。我会让你知道的。谢谢。非常感谢您的反馈。