admin管理员组文章数量:1201992
I have a custom post and custom taxonomy my taxonomy is called "department"
i have my loop showing my custom post, I am trying to also show all the departments (custom taxonomy) that my custom post falls under.
My different post are all set different departments however I am only seeing the same department on each post, not the department related to the post.
Am i missing something to show this ?
<ul>
<?php
$loop = new WP_Query(array('post_type' => 'career', 'posts_per_page' => 10));
$taxonomy = get_queried_object();
while ($loop->have_posts()) :
$loop->the_post(); ?>
<li>
<p><?php echo $term->name; ?></p>
<?php
echo 'Department', $taxonomy->name;
the_title();
$link = get_field('job_link');
if ($link):
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
?>
<a class="button" href="<?php echo esc_url($link_url); ?>"
target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>
<?php endif;
?>
<p>Location: <?php the_field('location'); ?></p>
</li>
<?php endwhile; ?>
</ul>
I have a custom post and custom taxonomy my taxonomy is called "department"
i have my loop showing my custom post, I am trying to also show all the departments (custom taxonomy) that my custom post falls under.
My different post are all set different departments however I am only seeing the same department on each post, not the department related to the post.
Am i missing something to show this ?
<ul>
<?php
$loop = new WP_Query(array('post_type' => 'career', 'posts_per_page' => 10));
$taxonomy = get_queried_object();
while ($loop->have_posts()) :
$loop->the_post(); ?>
<li>
<p><?php echo $term->name; ?></p>
<?php
echo 'Department', $taxonomy->name;
the_title();
$link = get_field('job_link');
if ($link):
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
?>
<a class="button" href="<?php echo esc_url($link_url); ?>"
target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>
<?php endif;
?>
<p>Location: <?php the_field('location'); ?></p>
</li>
<?php endwhile; ?>
</ul>
Share
Improve this question
asked Apr 13, 2022 at 17:13
BeepBeep
297 bronze badges
1 Answer
Reset to default 1You can use get_the_terms() to retrieve terms from a custom taxonomy which are attached to a post.
In your Loop you could do the following to retrieve the terms,
$departments = get_the_terms( get_the_ID(), 'department' );
This gives you an array of WP_Term
s. If for example, you want to use just the first one you could do it like this.
echo ($departments && is_array($departments) ? $departments[0]->name : '';
本文标签: phpShow current custom taxonomy
版权声明:本文标题:php - Show current custom taxonomy 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738628361a2103596.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论