【问题标题】:Add wordpress taxonomy term slug as a class to terms将 wordpress 分类术语 slug 作为一个类添加到术语中
【发布时间】:2016-07-20 10:56:02
【问题描述】:

我有以下问题。对于我正在进行的项目,我正在使用 get_the_term_list 来回显来自特定帖子和分类的所有术语。不,我需要能够为每个以不同方式呼应的术语设置样式。为此,我想将 term->slug 作为一个类添加到列表项中,但我无法使其工作。我知道如何将 slug 添加到 ul 中,但这不是我需要的,因为这样我就无法根据它们的 slug 设置各个列表项的样式。

所以此时我正在使用它来获取基于当前帖子的所有条款:

<?php echo get_the_term_list( $post->ID, 'thema',  '<ul class="project-themes no-bullet"><li>', '</li><li>', '</li></ul>'  ); ?>

是否有人知道如何在 li 上获取术语的 slug。所以我得到这样的东西:

<?php echo get_the_term_list( $post->ID, 'thema',  '<ul class="project-themes no-bullet"><li class="term->slug">', '</li><li class="term->slug">', '</li></ul>'  ); ?>

希望有人可以帮助我。

【问题讨论】:

    标签: wordpress custom-taxonomy


    【解决方案1】:

    这里的“get_the_term_list”会很棘手。相反,你可以试试这个---

      $terms = get_the_terms( $post->ID, 'your-taxonomy' );
    
      foreach($terms as $term){
      echo $term->slug;
      }
    

    【讨论】:

    • 感谢这个解决方案。忘记了有时候事情就是这么简单。
    【解决方案2】:

    将其修复为 @Mickey 的简单而有效的解决方案。结束了这个让它像我想要的那样工作:

    <?php $terms = get_the_terms( $post->ID, 'thema' ); ?>
    
    <ul class="project-themes no-bullet">
        <?php foreach($terms as $term) { ?>
            <li class="<?php get_term_link( $term->slug); ?>">
                <a href="<?php echo get_term_link( $term->term_id ); ?>"><?php  echo $term->slug; ?></a>
            </li>
        <?php } ?>
    </ul>
    

    再次感谢

    【讨论】:

      猜你喜欢
      • 2019-06-09
      • 1970-01-01
      • 2014-08-16
      • 2018-10-29
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多