帖子的While-loop中的类别重复

时间:2013-08-30 08:26:46

标签: php wordpress while-loop categories array-filter

我不想发现任何错误或任何内容,我只是不善于编码。

这段代码有什么用,在第四级找到我的类别,并打印出描述:

<?php while ( have_posts() ) : 
        the_post();
        $array = get_the_terms( get_the_ID(), 'product_category' );
        $fourth_level = array_filter($array, function ($t) {        
            if($t->parent != 0 
                && get_term($t->parent, 'product_category')->parent  != 0
                && get_term(get_term($t->parent, 'product_category')->parent, 'product_category')->parent != 0
                && get_term(get_term(get_term($t->parent, 'product_category')->parent, 'product_category')->parent, 'product_category')->parent == 0) return true;
            else return false;
        });

        foreach($fourth_level as $company)
        {
            if(isset($company))
            {
                // Print out company info \\
                echo "<a href='http://stilius.se/wilink/store/products/category/" . $company->slug . "/?cat=" . $_GET["cat"] . "'>" . $company->description . "</a>";
            }
        }

代码的问题是:每次类别(公司)有多个帖子(产品)时,代码都会打印出多个类别描述。

是否有一种简单的方法来检查类别说明是否已经打印出来然后阻止打印所述类别,或者检查同一类别中是否有多个帖子然后只打印一次?

提前致谢!

0 个答案:

没有答案