在get_the_term_list输出中添加一个类

时间:2019-06-15 13:12:18

标签: php html css wordpress output

我有以下代码:

<?php echo get_the_term_list($post->ID, 'themes', '', '', ''); ?>

哪个给我以下输出:

<a href="https://www.domain.test/themes/dark/" rel="tag">Dark</a>
<a href="https://www.domain.test/themes/white/" rel="tag">White</a>

问题是...如何向输出添加类?我希望结果是类似...

<a href="https://www.domain.test/themes/dark/" class="custom" rel="tag">Dark</a>
<a href="https://www.domain.test/themes/white/" class="custom" rel="tag">White</a>

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您需要使用自定义循环来添加类。

  <?php
    $themes = get_the_terms( $post->ID, 'themes');
    if ($themes && ! is_wp_error($themes)): ?>
        <?php foreach($themes as $theme): ?>

            <!-- The slug is the URL of taxonomy,  defined at the time of registration taxonomy within 'rewrite'.-->
            <a href="<?php echo get_term_link( $theme->slug, 'themes'); ?>" rel="tag" class="custom <?php echo $theme->slug; ?>"><?php echo $theme->name; ?></a>

        <?php endforeach; ?>
    <?php endif; ?>