admin管理员组

文章数量:1434916

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; ?>

本文标签: phpShow custom taxomony on custom single post typeStack Overflow