简体   繁体   English

如何在wordpress中获取特定分类法的术语?

[英]How get terms of specific taxonomy in wordpress?

I am working on properties project and need to get terms of specific taxonomy.我正在从事房地产项目,需要获得特定分类法的条款。 I mean properties of cities base of their states.我的意思是城市基础的属性。 Here is the code I have written for:这是我编写的代码:

<div class="row">
            <div class="col-md-2">
                <?php 
                    $taxonomy = 'property-state';
                    $terms = get_terms($taxonomy); 
                    ?>
                        <ul>
                            <?php foreach ( $terms as $term ) { ?>
                                <li>
                                    <a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
                                        <?php echo $term->name . '&nbsp;('. $term->count .')' ?>
                                    </a>
                                </li>
                            <?php } ?>
                        </ul>
                    <?php
                ?>
            </div>
            <div class="col-md-2">
                <?php 
                    $taxonomy = 'property-city';
                    $terms = get_terms($taxonomy); 
                    ?>
                        <ul>
                            <?php foreach ( $terms as $term ) { ?>
                                <li>
                                    <a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
                                        <?php echo $term->name . '&nbsp;('. $term->count .')'?>
                                    </a>
                                </li>
                            <?php } ?>
                        </ul>
                    <?php
                ?>
            </div>
        </div> <!-- end of row -->

What I need is to list properties of cities base of state, how should I change these codes?我需要的是列出州基础城市的属性,我应该如何更改这些代码?

I found my answer myself, I wanted to list properties of specific state with specific status.我自己找到了答案,我想列出具有特定状态的特定状态的属性。 for example the properties of Kabul state with status of (for-rent) and the properties count on each city of that state.例如,状态为(出租)的喀布尔州的财产以及该州的每个城市的财产。

<?php $terms = get_terms("property-city");

                foreach($terms as $term)
                {

                 
                $items = get_posts(array(
                        'numberposts'      => -1,
                        'post_type'        => 'property',
                        'post_status'      => 'publish',
                        'tax_query' => array(
                            'relation' => 'AND',
                            array(
                                'taxonomy' => 'property-city',
                                'field'    => 'slug',
                                'terms'    => $term->slug
                            ),
                            array(
                                'taxonomy' => 'property-state',
                                'field'    => 'name',
                                'terms'    => 'هرات',
                                'operator' => 'NOT IN'
                            ),
                        )
                    ));

                $count =  count( $items );
                ?>
                    <ul>
                        <li>
                            <a href="<?php echo get_term_link($term);?>" <?php if ($count == 0) echo " style='display: none';"; ?>>
                                <?php echo $term->name. '&nbsp;('. $count .')';?>
                            </a>
                        </li>
                    </ul>
                <?php
                
                }
                
                ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM