想要仅显示自定义分类WordPress的特定父项的子项

问题描述 投票:0回答:1

需要帮助:我必须只显示自定义分类WordPress的特定父级的子条款,在我的例子中:分类名称:“region”,与此相关:父条款及其子条:欧洲: - 葡萄牙; - 德国; - 英格兰;

亚洲: - 中国; - 日本;

因此,例如我需要在列表中仅显示欧洲的孩子,我该怎么做?我尝试了很多方法,只能显示所有父母的所有孩子:

        <?php
        $taxonomyName = "region";
        //This gets top layer terms only.  This is done by setting parent to 0.
        $parent_terms = get_terms( $taxonomyName, array( 'parent' => 0, 'orderby' => 'slug', 'hide_empty' => false ) );
        echo '<ul>';
        foreach ( $parent_terms as $pterm ) {
            //Get the Child terms
            $terms = get_terms( $taxonomyName, array( 'parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false ) );
            foreach ( $terms as $term ) {
                echo '<li><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></li>';
            }
        }
        echo '</ul>';
    ?>

但我只需要为特定的父母显示。谢谢您的帮助

wordpress custom-taxonomy taxonomy-terms
1个回答
0
投票

你已经有了答案。只需设置您的父术语并摆脱顶部嵌套的foreach。

 <?php
    $taxonomyName = "region";
    //Could use ACF or basic custom field to get the "parent tax ID" dynamically from a page. At least that's what I would do.
    $parent_tax_ID = '3';
    $parent_tax = get_term($parent_tax_ID);
    echo '<h3>' . $parent_tax->name . '</h3>';
    echo '<ul>';
    $terms = get_terms( $taxonomyName, array( 'parent' => $parent_tax_ID, 'orderby' => 'slug', 'hide_empty' => false ) );
    foreach ( $terms as $term ) {
        echo '<li><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></li>';
    }
    echo '</ul>';
?>
© www.soinside.com 2019 - 2024. All rights reserved.