最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to display singular_name of custom taxonomy?

programmeradmin1浏览0评论

I'm displaying list of term from each taxonomy assigned to custom post:

<?php $taxonomies = get_object_taxonomies( $post );
foreach ( $taxonomies as $taxonomy ) {

the_terms( $post->ID, $taxonomy, '<span class="e-article__category__item"><strong>' . SINGULAR_NAME . ': </strong>  ', ", ", '</span>' );
} ?>

but here, in SINGULAR_NAME I would like to display the singular_name of custom taxonomy.

I was trying this:

<?php $taxonomies = get_object_taxonomies( $post );
foreach ( $taxonomies as $taxonomy ) {

$term_name = $taxonomy->labels->singular_name;

the_terms( $post->ID, $taxonomy, '<span class="e-article__category__item"><strong>' . $term_name . ': </strong>  ', ", ", '</span>' );
} ?>

What I want to do is show all terms, separately for each taxonomy, but before each list I want to display taxonomy name (singular_name).

How to do that correctly?

Thanks :)

I'm displaying list of term from each taxonomy assigned to custom post:

<?php $taxonomies = get_object_taxonomies( $post );
foreach ( $taxonomies as $taxonomy ) {

the_terms( $post->ID, $taxonomy, '<span class="e-article__category__item"><strong>' . SINGULAR_NAME . ': </strong>  ', ", ", '</span>' );
} ?>

but here, in SINGULAR_NAME I would like to display the singular_name of custom taxonomy.

I was trying this:

<?php $taxonomies = get_object_taxonomies( $post );
foreach ( $taxonomies as $taxonomy ) {

$term_name = $taxonomy->labels->singular_name;

the_terms( $post->ID, $taxonomy, '<span class="e-article__category__item"><strong>' . $term_name . ': </strong>  ', ", ", '</span>' );
} ?>

What I want to do is show all terms, separately for each taxonomy, but before each list I want to display taxonomy name (singular_name).

How to do that correctly?

Thanks :)

Share Improve this question edited Jun 12, 2019 at 20:20 Piotr Milecki asked Jun 12, 2019 at 19:55 Piotr MileckiPiotr Milecki 253 bronze badges 2
  • Hi Piotr, what do you mean by "singular_name of CPT"? What exactly do you want to print? Could you update your question and add the effect you want to achieve? – Krzysiek Dróżdż Commented Jun 12, 2019 at 20:18
  • Cześć, Krzysztof:) I mean singular_name of custom taxonomy, my mistake :) – Piotr Milecki Commented Jun 12, 2019 at 20:20
Add a comment  | 

2 Answers 2

Reset to default 0

As far as I'm aware, your question has been answered here.

You're getting the singular_name correctly, all you need to do is echo it.

<?php $taxonomies = get_object_taxonomies( $post );
foreach ( $taxonomies as $taxonomy ) {

$term_name = $taxonomy->labels->singular_name;

echo $term_name;

the_terms( $post->ID, $taxonomy, '<span class="e-article__category__item"><strong>' . $term_name . ': </strong>  ', ", ", '</span>' );
} ?>

I finally get this done:

<?php $taxonomies = get_object_taxonomies( $post, 'objects' );
foreach ( $taxonomies as $taxonomy ) {
$tax_name = $taxonomy->labels->singular_name;
the_terms( $post->ID, $taxonomy->name, '<span class="e-article__category__item"><strong>' . $tax_name . ': </strong>  ', ", ", '</span>' );
} ?>

thanks :)

发布评论

评论列表(0)

  1. 暂无评论