如果未与其他自定义分类相关联,则隐藏自定义分类标题

时间:2015-09-24 作者:Anthony Hubble

自定义贴子类型命名业务(自定义字段(联系人详细信息,业务名称))业务类型分类法(他们做什么(清洁,自动))业务位置分类法;示例墨尔本-->南墨尔本,珀斯-->北珀斯

我想做的是你点击一种类型的业务(清洁),在该页面上,它将显示父位置标题(墨尔本),子位置标题(南墨尔本),以及与我点击的业务类型(清洁)相关的所有业务标题。

我有这一切的工作,但它显示的位置的业务母公司(墨尔本),即使它是没有关联的类别(清洁)我正在查看的任何职位。我怎么才能把这些藏起来?当前正在get\\u terms中循环get\\u terms,而不是调用帖子类型标题。

1 个回复
SO网友:Anthony Hubble

经过几个小时的搜索,我在另一个网站上找到了我的答案http://wpquestions.com/question/showChrono/id/10819

<?php

    // List posts by the terms for a custom taxonomy of any post type



        $post_type = \'product\';

        $tax = \'products\';

        $tax_args = array(

            \'order\' => \'DESC\',

            \'parent\' => 0

        );

        // get all the first level terms only

        $tax_terms = get_terms( $tax, $tax_args );

        if ($tax_terms) {

            foreach ($tax_terms  as $tax_term) { // foreach first level term

                // print the parent heading

                ?>

                <h4 class="parent-term"><?php echo $tax_term->name; ?></h4>



                <?php

                // get all its children

                $child_terms = ""; // first ensure this var is empty

                $child_terms = get_terms ( $tax, array(\'order\' => \'DESC\', \'parent\' => $tax_term->term_id) );

                // store an array of child terms slug

                $child_terms_array = array();

                foreach ($child_terms as $child_term){

                    $child_terms_array[] = $child_term->slug;

                }



                // first of all, print the posts of the parent, but excluding the one which are also into a child term

                $parent_args="";            

                $parent_args = array(

                    \'post_type\' => $post_type,

                    \'tax_query\' => array(

                        \'relation\' => \'AND\',

                        array(

                            \'taxonomy\' => $tax,               

                            \'field\' => \'slug\',                    

                            \'terms\' => $tax_term->slug,    

                            \'include_children\' => false,           

                            \'operator\' => \'IN\'                   

                          ),

                        array(

                            \'taxonomy\' => $tax,               

                            \'field\' => \'slug\',                    

                            \'terms\' => $child_terms_array,    

                            \'include_children\' => false,           

                            \'operator\' => \'NOT IN\'                   

                          )

                        ),

                    \'post_status\' => \'publish\',

                    \'posts_per_page\' => 6,

                    \'order\' => \'ASC\',

                );

                // query the posts

                $parent_query = null;

                $parent_query = new WP_Query($parent_args);



                if( $parent_query->have_posts() ) : ?>

                    <ul>

                        <?php while ( $parent_query->have_posts() ) : $parent_query->the_post(); ?>

                            <li><a href="<?php the_permalink() ?>">

                            <?php the_title(); ?>

                            </a>

                            </li>

                        <?php endwhile; // end of loop ?>       

                    </ul>



                <?php endif; // if have_posts()

                wp_reset_query();



                // if any, foreach child term, query the posts

                if ( !empty($child_terms) ){            

                    foreach ($child_terms as $child_term){

                        $child_args="";         

                        $child_args = array(

                            \'post_type\' => $post_type,

                            \'tax_query\' => array(

                                array(

                                    \'taxonomy\' => $tax,               

                                    \'field\' => \'slug\',                    

                                    \'terms\' => $child_term->slug,    

                                    \'include_children\' => false,           

                                    \'operator\' => \'IN\'                   

                                  )

                                ),

                            \'post_status\' => \'publish\',

                            \'posts_per_page\' => 6,

                            \'order\' => \'ASC\',

                        );

                        // query the posts  

                        $child_query = null;

                        $child_query = new WP_Query($child_args);



                        if( $child_query->have_posts() ) : ?>



                            <h4 class="child-term"><?php echo $child_term->name; ?></h4>    

                            <ul>

                                <?php while ( $child_query->have_posts() ) : $child_query->the_post(); ?>

                                    <li><a href="<?php the_permalink() ?>">

                                    <?php the_title(); ?>

                                    </a>

                                    </li>

                                <?php endwhile; // end of loop ?>



                            </ul>



                        <?php endif; // if have_posts()



                        wp_reset_query();

                    } // end foreach #child_terms

                }



            } // end foreach #parent_term

        }

        ?>