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 | Show 2 more comments2 Answers
Reset to default 0So 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
版权声明:本文标题:Adding Advanced Custom Fields to posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745541559a2662517.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$term = get_term( get_field('country') );
– Sally CJ Commented Apr 29, 2019 at 7:56