php - 添加一个类到 get_the_term_list 输出

标签 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>

提前致谢!

最佳答案

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

  <?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; ?>

关于php - 添加一个类到 get_the_term_list 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56610705/

相关文章:

php - 发布到两个链接表

PHP 是否可以识别包含对象的变量的名称?

javascript - 如何只使 Div 可滚动而不是窗口?

html - 使用 SVG 或 JPG/PNG 图像来提高页面性能是否更好?

javascript - 当 div 为空时 CSS 更改类

php - 复杂的 CakePHP 数据返回和组合

PHP 用方法调用查询

javascript - React hook useRef() 是如何工作的?那个引用到底是什么?

css - 是否可以在样式表中强制使用 "font priority"?

javascript - 谷歌自定义搜索搜索框不显示