Hello,
I try to display the taxonomy(ies) “auteur” and its link from a CPT called “membre” to regular posts. I read the documention about relationship but there’s nothing about this. the relationship field is called “article-membre”.
My code is the following (in the loop of index.php) :
<?php $auteurs = get_field('article-membre'); ?>
<?php if( $auteur ): ?>
<ul>
<?php foreach( $auteurs as $auteur ): ?>
<?php setup_postdata($auteur); ?>
<li>
<a href="<?php echo get_permalink( $auteur->ID ); ?>"><?php echo get_the_title( $auteur->ID ); ?></a>
<?php echo get_field( 'auteur', $auteur->ID ); // name of the taxonomy but it can't display the terms ?>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
Could you help me please ?
Best regards.
What is the field type and settings for the field you’re trying to get on this line?
<?php echo get_field( 'auteur', $auteur->ID ); // name of the taxonomy but it can't display the terms ?>
<
Hello John,
In fact, “auteur” is a Custom taxonomy for the CPT “membres”. It’s not a field…
But you’re using get_field to get the values, so I’m assuming that you have an ACF taxonomy field set up for it. Is that correct or not?
No, haven’t this field.
But if i create this field, i have twice checkboxes in CPT “membres” : this one of the Custom taxonomy and this one of the taxonomy field
Then, is it possible to do without the taxonomy field and only use the custom taxonomy ?
If you use a taxonomy field then you can look at this documentation on how to display the information from the taxonomy http://www.advancedcustomfields.com/resources/taxonomy/
If you don’t use a taxonomy field and use the standard WP taxonomy box then you’ll need to look at this https://codex.wordpress.org/Function_Reference/wp_get_post_terms
Ok, it works with :
<?php $terms = get_the_terms( $auteur->ID, 'auteur' );
foreach($terms as $term) {
$term_link = get_term_link( $term );
echo '<a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>';
} ?>
Thank you John
The topic ‘Relationship : CPT taxonomy and regular post’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.