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
Add a comment  | 

1 Answer 1

Reset to default 1

You 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_Terms. 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