【问题标题】:Combine multiple custom user taxonomy in single url在单个 url 中组合多个自定义用户分类
【发布时间】:2016-03-24 22:02:21
【问题描述】:

我已经为用户创建了自定义用户分类,它适用于单一分类。

根据 Bellow 参考指南,我创建了自定义用户分类。

参考指南:-http://justintadlock.com/archives/2011/10/20/custom-user-taxonomies-in-wordpress

下面是分类结构

(registered taxonomy)  profession

                              Dietitian (under the profession )



(registered taxonomy) city
                         NewYork(under the city)

我的蛞蝓名字expert

在这里,我想在NewYork city.so 中显示用户为Dietitian 的过滤结果。因此它将显示在配置文件中选择了上述选项的所有用户。

现在我想要类似www.test.com/expert/dietitian/newyork 的网址 在用户个人资料中选择了营养师,纽约的用户。有没有什么办法可以结合Dietitian,NewYork

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

【问题讨论】:

标签: wordpress custom-taxonomy


【解决方案1】:

我假设您在justintadlock 的教程上花费了大量时间,所以我将只涉及其中的多分类过滤和前端显示所需的部分。

在阅读长文之前,您可以在http://playground.georgemanousarides.com/尝一尝

所以,第一件事就是第一。 SINGLE TAXONOMY 和 MULTIPLE TAXONOMIES 的逻辑基础相同。 在注册分类法及其条款并完成 wp-admin 所需的所有工作后,我们将需要:

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

B)另一个页面(我们称之为TAX-RESULTS PAGE)将显示TAX-QUERY PAGE的结果。

让我们潜入


单一分类法

税务查询页面

按照教程进行操作后,显示分类链接的页面的原始形式应如下所示:

single taxonomy tax-query page

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

请注意:

A)以下链接:

  1. 职业将是这样的: www.example.com/user/profession/developer/
  2. 城市将是这样的: www.example.com/user/city/gotham/

B) slug "user/city" 和 "user/profession" 在函数中定义:my_register_user_taxonomy >> register_taxonomy >> rewrite >> slug

税务结果页面

点击上面提到的链接会带你进入一个页面(当然需要你自己定义),该页面显示了同一分类术语下的所有用户。

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

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

在我看来,如果你不希望你的服务器被你手动创建的模板文件淹没,你应该使用 taxonomy.php 并为所有分类法制作一个通用的模板来描述你想要的结果自动显示。

请注意:

A)要遍历用户并仅获取所需分类术语下的用户,需要自定义查询(在 taxonomy.php 文件中),如教程中所述和解释。

B) 永久链接应位于 wp-admin >> 设置 >> 永久链接的 帖子名称 选项上 以便 wordpress 可以找到您的分类法,获取文件 taxonomy.php 并提供有关可在 taxonomy.php 文件中使用的所选分类法的有用信息。


多种分类法

税务查询页面

我们需要创建一个页面,访问者可以在其中从自定义分类中选择术语。此页面将通过链接(作为获取变量**)向 TAX-RESULTS PAGE (taxonomy.php) 提供所需的术语,以便进行查询。

**为了在 TAX-RESULTS PAGE 中有漂亮的永久链接,我们需要在 function.php 中添加以下重写函数(找到它here),并在 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' );

请注意

如果你注册了更多的分类,函数中的“profession|city”应该变成“profession|city|tax1|tax2|tax3”。

因此,TAX-QUERY PAGE 将是:

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>

JQUERY(可以放在外部文件中)

$( document ).ready( function() {
    $( '#multiTaxSubmit' ).click( function ( event ){
        event.preventDefault(); //prevent default link url from loading
        var taxQuerySubmit = $( this ),
                hasChecked = 0;
                querySlug = taxQuerySubmit.attr( 'href' ); //get multitax url base from link href attr
        $( '#taxonomies fieldset' ).each( function() { //iterate each taxonomy
            var checkedTerms = $( this ).find( 'input:checked' ),
                    checkedLength = checkedTerms.length; //how many terms has the user selected
            if ( checkedLength ) {
                hasChecked += checkedLength;
                querySlug += '/' + $( this ).attr( 'id' ) + '/'; //add taxonomy slug to query url
                checkedTerms.each( function( index, value ) {
                    var comma = ( index == checkedLength-1 ? '' : ',' );
                    querySlug += $( this ).val() + comma;
                } );
            }
        } );
        if ( hasChecked ) {
            window.location = querySlug;
        } else {
            alert( 'Please enter some criteria.' );
        }
    } );
} );

税务结果页面 (taxonomy.php)

<?php
    $USERS_BY_TAX = array();
    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 $tax_key => $taxonomy ) {
            eval( '$check = $' . $taxonomy . ';' ); // Check if the taxonomy exists in the url. eval outputs $check = $profession, $check = $city etc.
            if ( !$check ){
                unset( $taxonomies[ $tax_key ] );
                continue;
            }
            eval( '$term_names = explode( ",", $' . $taxonomy . ' );' ); // get terms array from $$taxonomy which gives $profession,$city, the values of which are given through the url as such: $profession="designer,developer"
            $USERS_BY_TAX[ $taxonomy ] = array();
            foreach ( $term_names as $term_name ) {
                $term_obj = get_term_by( 'name', $term_name, $taxonomy ); //get term object for each given term
                $users_in_term = get_objects_in_term( $term_obj -> term_id, $taxonomy ); // find users with term
                if ( !empty( $users_in_term ) ) {
                    $USERS_BY_TAX[ $taxonomy ] = $USERS_BY_TAX[ $taxonomy ] + array_fill_keys( $users_in_term, $term_name ) ;
                }
            }
        }
    }
    /* $USERS_BY_TAX array has all the users for each taxonomy but we only need those who exist in all taxonomies */
    if ( $taxonomies ) {
        $RESULTS = $USERS_BY_TAX; // keep the initiate array intact
        $matched = array_pop( $USERS_BY_TAX ); // first array to compare
        $TAXS = $taxonomies;
        array_pop( $taxonomies );
        if ( !empty( $USERS_BY_TAX ) ) {
            foreach ( $taxonomies as $taxonomy ) {
                if ( !empty( $USERS_BY_TAX ) ) $matched = array_intersect_key( $matched, $USERS_BY_TAX[ $taxonomy ] );
            }
        }
    }
?>

/* DISPLAY */
<?php if ( $matched ) :
    foreach ( array_keys( $matched ) as $user_id ): ?>

        <div class="user-entry">
            <?php echo get_avatar( get_the_author_meta( 'email', $user_id ), '96' ); ?>
            <h2><?php the_author_meta( 'display_name', $user_id ); ?></h2>
            <?php if ( in_array( 'profession', $TAXS ) ) : ?><h3>Profession: <?php echo $RESULTS[ 'profession' ][ $user_id ]; ?></h3><?php endif;?>
            <?php if ( in_array( 'city', $TAXS ) ) : ?><h3>City: <?php echo $RESULTS[ 'city' ][ $user_id ]; ?></h3><?php endif;?>
            <?php echo wpautop( get_the_author_meta( 'description', $user_id ) ); ?>
        </div>

    <?php endforeach;
else: ?>
        <div class="user-entry">
            <h2>We are sorry. No results match your criteria.</h2>
            <h3>Please <a href="javascript:history.back()">go back</a> and search again!</h3>
        </div>
<?php endif; ?>

【讨论】:

  • 因为您的演示链接看起来是一个正确的答案,但我需要一些时间在现场实施,现在正忙于其他任务。
  • @SanjayNakate 运气好吗?
  • 我没有尝试忙于其他任务。会让你知道的。
  • 谢谢。非常感谢您的反馈。
【解决方案2】:

这个重写规则应该有效(假设“专业”和“城市”是分类注册名称):

代码:

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

记住在您的站点中保存此代码后刷新重写规则。

网址: http://yourdomain.com/profession/dietitian/city/newyork/

【讨论】:

    【解决方案3】:

    要从您的主题或插件中刷新永久链接或重写规则,您需要使用 flush_rewrite_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');
    ?>
    

    【讨论】:

      猜你喜欢
      • 2016-10-29
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 2022-01-22
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 2017-07-16
      相关资源
      最近更新 更多