You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress特定文章自定义层级分类展示问题求助

Solution for Hierarchical Custom Category Display (Only Post-Assigned Terms)

Got it, let's tackle this problem. The key here is to only work with the terms attached to your post (not all terms in the listing_category taxonomy) and then organize them into their parent-child hierarchy.

Step-by-Step Approach

  1. Group the post-attached terms by their parent ID to map child terms to their respective parents
  2. Use a recursive function to render the terms in a nested, indented structure (or nested HTML list) instead of a flat string

Working Code Snippet (Indented Text)

Replace your existing code with this to get an indented hierarchical display:

<?php
$post_id = get_the_ID(); // Use your existing $post_id variable if already defined
$terms = get_the_terms( $post_id, 'listing_category' );

if ( $terms && !is_wp_error( $terms ) ) {
    // Group terms by their parent ID to build hierarchy
    $term_groups = array();
    foreach ( $terms as $term ) {
        $parent_id = $term->parent > 0 ? $term->parent : 0;
        if ( !isset( $term_groups[$parent_id] ) ) {
            $term_groups[$parent_id] = array();
        }
        $term_groups[$parent_id][] = $term;
    }

    // Recursive function to render nested terms with indentation
    function render_nested_terms( $term_group, $term_groups, $depth = 0 ) {
        $output = '';
        $indent = str_repeat( '&nbsp;&nbsp;', $depth ); // Adjust number of spaces for indent size

        foreach ( $term_group as $term ) {
            // Add current term with appropriate indent
            $output .= $indent . esc_html( $term->name ) . '<br>';

            // Render child terms (only if they're attached to the post)
            if ( isset( $term_groups[$term->term_id] ) ) {
                $output .= render_nested_terms( $term_groups[$term->term_id], $term_groups, $depth + 1 );
            }
        }
        return $output;
    }

    // Start rendering from top-level parent terms (parent ID = 0)
    $categories_list = render_nested_terms( $term_groups[0] ?? array(), $term_groups );
?>
<tr>
    <td><strong>Categories: </strong></td>
    <td><?php echo $categories_list; ?></td>
</tr>
<?php } ?>

Alternative: Nested HTML List (Stylable with CSS)

If you want a more structured, styleable output, modify the render_nested_terms function to generate nested <ul>/<li> elements:

function render_nested_terms( $term_group, $term_groups ) {
    if ( empty( $term_group ) ) return '';

    $output = '<ul>';
    foreach ( $term_group as $term ) {
        $output .= '<li>' . esc_html( $term->name );
        // Render child terms attached to the post
        if ( isset( $term_groups[$term->term_id] ) ) {
            $output .= render_nested_terms( $term_groups[$term->term_id], $term_groups );
        }
        $output .= '</li>';
    }
    $output .= '</ul>';
    return $output;
}

Key Notes

  • This solution only includes terms that are directly attached to your post—no extra terms from the taxonomy will be displayed.
  • The recursive function ensures child terms are nested under their parent terms, creating a clear hierarchy.
  • All output is escaped with esc_html() to maintain WordPress security best practices.

内容的提问来源于stack exchange,提问作者mobius2000

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 13:14:08