0

i'm using Advanced Custom Fields plugins and i want to display the fields on my posts. So i modified the single.php file of my theme like this:

<ul>
    <li><?php the_field('name_of_placement'); ?></li>
    <li><?php the_field('country'); ?></li>
    <li><?php the_field('timeframe'); ?></li>
    <li><?php the_field('types_of_healthcare_placement'); ?></li>
</ul>

And this is my test post:

http://electives-abroad.org/custom-field-test/

The only field that displays correctly is the "name of placement" field, the others display numbers, i don't know why.

UPDATE

The 'country' field is a taxonomy. According to this: advancedcustomfields.com/resources/taxonomy

I put the following:

<?php 

$term = get_field('country');

if( $term ): ?>

    <h2><?php echo $term->name; ?></h2>
    <p><?php echo $term->description; ?></p>
    <p>Color: <?php the_field('country', $term); ?></p>

But it doesn't show me anything –

*SOLUTION PROPOSED BY Sally CJ

    <h2><?php echo $term->name; ?></h2>
    <p><?php echo $term->description; ?></p>
    <p>Color: <?php the_field('country', $term); ?></p>

<?php endif; ?>
                <ul>
                    <li><?php the_field('name_of_placement'); ?></li>
                    <li><?php the_field('country'); ?></li>
                    <li><?php the_field('timeframe'); ?></li>
                    <li><?php the_field('types_of_healthcare_placement'); ?></li>
                </ul>

Now it shows me an "uncategorized".

New update

Now i tried this, to show 2 of my taxonomies: country and timeframe.

<ul>
                    <li><?php the_field('name_of_placement'); ?></li>
                    <li><?php echo get_term_field( 'name', get_field('country') ); ?></li>
                    <li><?php echo get_term_field( 'name', get_field('timeframe') ); ?></li>
                    <li><?php the_field('types_of_healthcare_placement'); ?></li>
                </ul>

Both of them appear as "uncategorized".

  • Seems like the other fields are not standard text fields. See the ACF field types and check which ones you used. – Sally CJ Apr 29 at 7:06
  • I updated the question. Please look. – Draws Ren Gundam Apr 29 at 7:50
  • To make your current code works, change the field's Return Value to Term Object. – Sally CJ Apr 29 at 7:53
  • Come again? i didn't understand. – Draws Ren Gundam Apr 29 at 7:55
  • Never mind. Try $term = get_term( get_field('country') ); – Sally CJ Apr 29 at 7:56
0

So if the "country" field is a taxonomy field with a single value (i.e. the field's Appearance is radio buttons or a single-selection select/drop-down menu), and you want to display the term name, then you could use the get_term_field() function in WordPress:

<li><?php echo get_term_field( 'name', get_field('country') ); ?></li>

Or using get_term():

<li><?php $term = get_term( get_field('country') ); echo $term->name; ?></li>

I hope that helps you.

UPDATE

ACF may not be using the proper post ID, so try replacing the get_field('country') with this one:

get_field( 'country', get_the_ID() )

So:

<li><?php echo get_term_field( 'name', get_field( 'country', get_the_ID() ) ); ?></li>

UPDATE 2

If you want to display the "country" terms/categories the post was assigned to, then:

<li><?php the_terms( get_the_ID(), 'country' ); ?></li>

would do it. You don't need ACF or any plugin to do that.. except maybe, you need a plugin for registering/creating the custom taxonomy and/or associating it to certain post types.

UPDATE 3

Or... when you create/edit the ACF field, make sure to select the proper taxonomy, which in your case, I guess it's "Country" or "Countries"? See the "Taxonomy" option below the "Required?" option on this image

  • I edited the main post again, please look. – Draws Ren Gundam Apr 29 at 10:27
  • On the back-end, when you select an option for the country field, which means you selected a term (if the field is indeed a Taxonomy field), what was the option's label? Is it "uncategorized"? Try var_dump( get_field('country'), get_field('country', get_the_ID()) ); - do you see same IDs? – Sally CJ Apr 29 at 11:02
  • object(WP_Term)#1826 (10) { ["term_id"]=> int(1) ["name"]=> string(13) "Uncategorized" ["slug"]=> string(13) "uncategorized" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(1) ["taxonomy"]=> string(8) "category" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(8) ["filter"]=> string(3) "raw" } – Draws Ren Gundam Apr 29 at 11:04
  • Okay, but just ignore that one and check my other comment or check the updated answer. – Sally CJ Apr 29 at 11:10
  • I did it and i get "Uncategorized" again. electives-abroad.org/custom-field-test – Draws Ren Gundam Apr 29 at 11:20
0

You can use default get_post_meta() WordPress function

get_post_meta( $post->ID,  'country', TRUE)

Hope this will help you!

  • Where do i put that? – Draws Ren Gundam Apr 29 at 10:29
  • In single.php <li><?= get_post_meta( $post->ID, 'country', TRUE); ?></li> – Parth Shah Apr 29 at 10:32
  • You can find current post id by get_the_ID() or use global $post for use $post->ID – Parth Shah Apr 29 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.