最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Show custom taxomony on custom single post type - Stack Overflow

matteradmin5PV0评论

Would love some help on a website I'm currently building because I seem to not get something to work. The website has a custom post type 'evenement'. I have created a custom taxonomy 'locatie' and last but not least I have made a Field group item 'evenement_locatie', linked to the custom taxonomy.

I made a template for that show's a single evenement post, that show's the custom fields which I get with this code

<p><?php the_field("event_startdate"); ?><br> om <?php the_field("event_starttime"); ?></p>

This works fine for all fields, however I can't seem to load the custom taxomony, and I tried all codes that I could find online. This is the code I'm currently using:

<?php $term = get_field('evenement_locatie');
if( $term ): ?>
<h2><?php echo esc_html( $term->name ); ?>test</h2>
<p><?php echo esc_html( $term->description ); ?></p>
<?php endif; ?>

And I have also tried

<?php echo get_field('evenement_locatie', get_queried_object()); ?>

Also tried to use the get_term function, but I can't figure out how to get it working. I also trying using get_field('locatie') and that does not work as well.

My goal is to show the taxonomy title, and the description, but I've tried for hours now to get something. Also the WP_DEBUG is not giving me any error's as well, so don't know how to continue.

Any tips or tricks? Thanks in advance!

Would love some help on a website I'm currently building because I seem to not get something to work. The website has a custom post type 'evenement'. I have created a custom taxonomy 'locatie' and last but not least I have made a Field group item 'evenement_locatie', linked to the custom taxonomy.

I made a template for that show's a single evenement post, that show's the custom fields which I get with this code

<p><?php the_field("event_startdate"); ?><br> om <?php the_field("event_starttime"); ?></p>

This works fine for all fields, however I can't seem to load the custom taxomony, and I tried all codes that I could find online. This is the code I'm currently using:

<?php $term = get_field('evenement_locatie');
if( $term ): ?>
<h2><?php echo esc_html( $term->name ); ?>test</h2>
<p><?php echo esc_html( $term->description ); ?></p>
<?php endif; ?>

And I have also tried

<?php echo get_field('evenement_locatie', get_queried_object()); ?>

Also tried to use the get_term function, but I can't figure out how to get it working. I also trying using get_field('locatie') and that does not work as well.

My goal is to show the taxonomy title, and the description, but I've tried for hours now to get something. Also the WP_DEBUG is not giving me any error's as well, so don't know how to continue.

Any tips or tricks? Thanks in advance!

Share Improve this question asked Nov 17, 2024 at 16:25 Casper BoonCasper Boon 5711 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I think the issue is when creating a taxonomy field in ACF, the default result value will be a term ID, not a term object. Your code is correct but it's only work with the term object only.

You can check the return value in ACF setting like this screenshot

There are 2 options here

  • Update the return value in ACF to Term Object so you can use the current code.
  • Use the new code below to get the field you need
<?php 
$term_id = get_field('evenement_locatie');
if($term_id):
    $term = get_term($term_id);
?>
    <h2><?php echo esc_html($term->name); ?>test</h2>
    <p><?php echo esc_html($term->description); ?></p>
<?php endif; ?>
Post a comment

comment list (0)

  1. No comments so far