【问题标题】:Add a class to get_the_term_list output向 get_the_term_list 输出添加一个类
【发布时间】:2019-06-15 13:12:18
【问题描述】:

我有以下代码:

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

提前致谢!

【问题讨论】:

  • 显示 'get_the_term_list' 函数,必须在此函数中进行更改
  • 您在使用 WordPress 吗?
  • @NickParsons 是的,我正在使用 WordPress。
  • @JacobK。你能显示get_the_term_list 函数吗?
  • @NickParsons 我找不到函数。 :(

标签: php html css wordpress output


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-12-03
    • 2012-02-02
    • 1970-01-01
    • 2020-05-11
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多