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
1 Answer
Reset to default 1You 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
版权声明:本文标题:custom taxonomy - How can I get the categories and subcategories separately? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288318a1928070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论