admin管理员组

文章数量:1122846

I am using this code to get the categories and subcategories from the reference page. It is quite good because it allows me to sort them hierarchically. But I would like to give them two different styles (one for the categories and one for the subcategories). How can I do that?

        <ul class="chip-list">
            <?php $categories = wp_get_post_terms( get_the_id(), 'portfolio_category', array( 'orderby' => 'term_order'));
            if( $categories ): ?>
                <?php foreach( $categories as $category ): ?>
                <li>
                    <a class="chip chip__portfolio--category" href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
                        <?php echo $category->name; ?>
                    </a>
                </li>
                <?php endforeach; ?>
            <?php endif; ?>
        </ul>

I am using this code to get the categories and subcategories from the reference page. It is quite good because it allows me to sort them hierarchically. But I would like to give them two different styles (one for the categories and one for the subcategories). How can I do that?

        <ul class="chip-list">
            <?php $categories = wp_get_post_terms( get_the_id(), 'portfolio_category', array( 'orderby' => 'term_order'));
            if( $categories ): ?>
                <?php foreach( $categories as $category ): ?>
                <li>
                    <a class="chip chip__portfolio--category" href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
                        <?php echo $category->name; ?>
                    </a>
                </li>
                <?php endforeach; ?>
            <?php endif; ?>
        </ul>
Share Improve this question asked Aug 3, 2024 at 7:35 Antonino LatteneAntonino Lattene 1431 gold badge2 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You must modify your code to set different stylings for parent and child categories. Here is an updated approach for you.

<ul class="chip-list">
    <?php 
    $categories = wp_get_post_terms( get_the_ID(), 'portfolio_category', array( 'orderby' => 'term_order' ) );
    if ( $categories ): ?>
        <?php foreach ($categories as $category): ?>
            <li>
                <?php if ( $category->parent == 0 ): // Parent category ?>
                    <a class="chip chip__portfolio--category" href="<?php echo esc_url(get_category_link($category->term_id)); ?>">
                        <?php echo $category->name; ?>
                    </a>
                <?php else: // Subcategory ?>
                    <a class="chip chip__portfolio--subcategory" href="<?php echo esc_url(get_category_link($category->term_id)); ?>">
                        <?php echo $category->name; ?>
                    </a>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    <?php endif; ?>
</ul>

本文标签: custom taxonomyHow can I get the categories and subcategories separately