admin管理员组

文章数量:1429508

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:

/

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/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".

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/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/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".

Share Improve this question edited Apr 29, 2019 at 10:16 Draws Ren Gundam asked Apr 29, 2019 at 6:46 Draws Ren GundamDraws Ren Gundam 251 gold badge2 silver badges6 bronze badges 7
  • Seems like the other fields are not standard text fields. See the ACF field types and check which ones you used. – Sally CJ Commented Apr 29, 2019 at 7:06
  • I updated the question. Please look. – Draws Ren Gundam Commented Apr 29, 2019 at 7:50
  • To make your current code works, change the field's Return Value to Term Object. – Sally CJ Commented Apr 29, 2019 at 7:53
  • Come again? i didn't understand. – Draws Ren Gundam Commented Apr 29, 2019 at 7:55
  • Never mind. Try $term = get_term( get_field('country') ); – Sally CJ Commented Apr 29, 2019 at 7:56
 |  Show 2 more comments

2 Answers 2

Reset to default 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

You can use default get_post_meta() WordPress function

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

Hope this will help you!

本文标签: Adding Advanced Custom Fields to posts