我有以下循環,它會執行并列出自定義分類法。在本例中,它列出了我的自定義帖子類型“產品”中的所有“產品類別”。<!-- Product Categories =========================================== --> <?php $taxonomy = 'product_category'; $terms = get_terms($taxonomy); // Get all terms of a taxonomy if ( $terms && !is_wp_error( $terms ) ) : ?> <div class="container-flex"> <div class="row"> <?php foreach ( $terms as $term ) { $image = get_field('icon', $term ); $primarycolor = get_field('category_colour_primary', $term); $secondarycolor = get_field('category_colour_secondary', $term); $url = $image['url']; $title = $image['title']; $alt = $image['alt']; $size = 'large'; $thumb = $image['sizes'][ $size ]; ?> <style type="text/css"> .product-arrow-right:after { border-color: <?php echo $primarycolor; ?>; } .product-arrow-right:before { background-color: <?php echo $primarycolor; ?>; } </style>這個術語:$primarycolor = get_field('category_colour_primary', $term);似乎工作正常,因為我用它來給文本著色:<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>" style="color: <?php echo $primarycolor; ?>;">Find Out More</a><span class="product-arrow-right"></span>但是當我嘗試使用它來設置跨度的偽元素 :before 和 :after 的樣式時,它似乎將它們全部設置為拉過的顏色列表中的最后一種顏色...你會在這里看到它給箭頭著色,但它把它們全部著色為藍色,最后一個分類的顏色......
1 回答

慕村225694
TA貢獻1880條經驗 獲得超4個贊
嘗試添加一些計數器
$css_key = 0;
foreach ( $terms as $term ) {
$css_key++;
然后將此計數器添加到您的 css 選擇器中
<style type="text/css">
.product-arrow-right:nth-child(<?php echo $css_key; ?>):after { border-color: <?php echo $primarycolor; ?>; }
.product-arrow-right:nth-child(<?php echo $css_key; ?>):before { background-color: <?php echo $primarycolor; ?>; }
</style>
- 1 回答
- 0 關注
- 126 瀏覽
添加回答
舉報
0/150
提交
取消