显示多个类别名称 wordpress [英] Display multiple category name wordpress

查看:32
本文介绍了显示多个类别名称 wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望在特定帖子上显示 wordpress 帖子的所有类别名称.

Looking to display all category names for wordpress posts on a specific post.

因此,如果帖子属于网页设计、搜索引擎优化和社交媒体",我希望这些类别名称显示在帖子中 - 而不是所有其他类别,例如与应用设计"无关的类别.

So if a post is in "web design, seo and social media" i want these category names to display on the post - and NOT all the other categories such as "app design" which it is not related too.

<p>
    <?php $category = get_the_category();
    echo $category[0]->cat_name;
    echo " / ";
    echo $category[1]->cat_name;
    echo " / ";
    echo $category[2]->cat_name;
    ?>
</p>

这目前有效,但如果只有 1 个类别,那么我会在显示文本的末尾获得额外的/.

this works currently but if there is only 1 category then i get extra / on the end of the displayed text.

其他人可以使用更好的循环吗?

Can someone else with a loop that works better?

推荐答案

您的代码显示未定义 $count 变量,但是当我使用此代码时,它显示了一些类别.

Your code show undefines the $count variable but when I use this code it shows some categories.

  <?php
                  $categories = get_the_category();
                  $cat_id     = $categories[0]->term_id;

                  foreach ( $categories as $i => $category ) {
                      echo '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" >' . esc_html( $category->name ).'</a>';
                      if ( $i < 'count - 1' )
                          echo $separator;
                  }

               ?>

这篇关于显示多个类别名称 wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆