douduiwei2831 2013-08-30 08:26
浏览 25

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

I don't have want to find out a bug or anything, I'm just bad at coding.

What have have is this code, to find my categories in the fourth level, and print out the descriptions:

<?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>";
            }
        }

The problem with the code is this: Every time a category (company) has more than one post (product) the code prints out multiple category descriptions.

Would there be an easy way to just check if the category description is already printed out and then prevent the printing of said category, or check if a there are multiple posts in the same category and then only print it once?

Thanks in advance!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化