問題描述

我有一個分層分類稱為’geographical locations’ 。它包含一級大陸,然後是每個國家。示例:

Europe
- Ireland
- Spain
- Sweden
Asia
- Laos
- Thailand
- Vietnam

等等

使用 get_terms() 我設法輸出完整的術語列表,但是大陸與國家混在一起,在一個大的平面列表中。

如何輸出如上所述的分層列表?

最佳解決方案

使用 wp_list_categories 與分類參數,它用於創建層次類別列表,但也支持使用自定義分類法。

食典示例:Display terms in a custom taxonomy

如果列表看起來很平坦,那麼您可能只需要一點 CSS 來添加填充列表,因此您可以看到它們的層次結構。

次佳解決方案

我意識到這是一個非常古老的問題,但如果您需要建立一個實際的術語結構,這可能是一個有用的方法:

/**
 * Recursively sort an array of taxonomy terms hierarchically. Child categories will be
 * placed under a 'children' member of their parent term.
 * @param Array   $cats     taxonomy term objects to sort
 * @param Array   $into     result array to put them in
 * @param integer $parentId the current parent ID to put them in
 */
function sort_terms_hierarchicaly(Array &$cats, Array &$into, $parentId = 0)
{
    foreach ($cats as $i => $cat) {
        if ($cat->parent == $parentId) {
            $into[$cat->term_id] = $cat;
            unset($cats[$i]);
        }
    }

    foreach ($into as $topCat) {
        $topCat->children = array();
        sort_terms_hierarchicaly($cats, $topCat->children, $topCat->term_id);
    }
}

用法如下:

$categories = get_terms('my_taxonomy_name', array('hide_empty' => false));
$categoryHierarchy = array();
sort_terms_hierarchicaly($categories, $categoryHierarchy);

var_dump($categoryHierarchy);

第三種解決方案

我不知道任何你想要的功能,但你可以建立這樣的東西:

<ul>
    <?php $hiterms = get_terms("my_tax", array("orderby" => "slug", "parent" => 0)); ?>
    <?php foreach($hiterms as $key => $hiterm) : ?>
        <li>
            <?php echo $hiterm->name; ?>
            <?php $loterms = get_terms("my_tax", array("orderby" => "slug", "parent" => $hiterm->term_id)); ?>
            <?php if($loterms) : ?>
                <ul>
                    <?php foreach($loterms as $key => $loterm) : ?>
                        <li><?php echo $loterm->name; ?></li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </li>
    <?php endforeach; ?>
</ul>

我沒有測試過,但是你可以看到我在做什麼。上面的代碼將只給你兩個級別

編輯:啊是的,你可以使用 wp_list_categories() 做你以後的事情。

第四種方案

您可以使用 wp_list_categories() 與’taxonomy’ 參數。

第五種方案

以下代碼將使用術語生成 drop-down,還可以通過編輯 $ outputTemplate 變量生成任何其他元素/結構,並編輯 str_replace 行:

function get_terms_hierarchical($terms, $output = '', $parent_id = 0, $level = 0) {
    //Out Template
    $outputTemplate = '<option value="%ID%">%PADDING%%NAME%</option>';

    foreach ($terms as $term) {
        if ($parent_id == $term->parent) {
            //Replacing the template variables
            $itemOutput = str_replace('%ID%', $term->term_id, $outputTemplate);
            $itemOutput = str_replace('%PADDING%', str_pad('', $level*12, '&nbsp;&nbsp;'), $itemOutput);
            $itemOutput = str_replace('%NAME%', $term->name, $itemOutput);

            $output .= $itemOutput;
            $output = get_terms_hierarchical($terms, $output, $term->term_id, $level + 1);
        }
    }
    return $output;
}

$terms = get_terms('taxonomy', array('hide_empty' => false));
$output = get_terms_hierarchical($terms);

echo '<select>' . $output . '</select>';

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。