Home › Forums › General Issues › Trying to display a category (taxonomy) field in the fronted
Trying to display a category (taxonomy) field added to a custom post, for it’s single page. My code in the cntent template goes like this, but it return nothing.
<li>Plan type : <?php the_field('plan_type'); ?><li>
The field is configured like this (see pic attached).
If you are trying to show a field added to a term then you must supply the term or a term “post id”
the_field('plan_type', $term); // in this case a term object
or
the_field('plan_type', 'term_'.$term_id);
<?php the_field(‘plan_type’, ‘term_’.$term_id); ?> and <?php the_field(‘plan_type’, $term); ?> dont generate a content.
I think I don’t get it. Sorry 😉
Do I have to replace $term or $term_id by something else?
To make it clearer, plan_type is a field in ACF (a taxonomy field – i choosed category). Th field is inthe group field for the CPT. So ca category have to be choosed (different for every post).
I am trying to make the unique post (single post) template, showing the category choosed on the post unique page.
I thought you were trying to show a field on a term, not show the term in a tax field.
The documentation for a taxonomy field is here https://www.advancedcustomfields.com/resources/taxonomy/ and it gives examples on to use this field.
A taxonomy field is one of those fields that do not work well with the_field()
Thanks again.
Tried the 3 ways.. Doesn’t work. Echo nothing. 🙁
Here is code i tried for the first option (single object). I also attached a screenshot for the fiel settings in ACF.
<?php
$term = get_field(‘plan_type’);
if( $term ): ?>
<h2><?php echo esc_html( $term->name ); ?></h2>
<p><?php echo esc_html( $term->description ); ?></p>
<?php endif; ?>
Where exactly are you trying to do this?
What template?
Are you in “The Loop”?
I am in trying to call the category in the template part file:
X:\wp-content\themes\wp-bootstrap-starter\template-parts\content-plan.php
The template part is then called in a single page file I created for showing only one “plan” in the frontend:
\wp-content\themes\wp-bootstrap-starter\single-plan.php
Every fields are appearing in a single plan page on the frontend, appart the category taxonomy selected for the plan in question (plan_type field in ACF).
Template part code:
<?php
/**
* Template part: Plan
*/
if ( !function_exists( 'get_field' ) )
return;
?>
<h1><?php the_title(); ?> plan</h1>
<ul>
<li>Host : <?php
$companyname = get_field('companyname');
echo get_the_title( $companyname->ID );
?>
</li>
<li>Plan type : <?php the_field('plan_type'); ?></li>
<li>Monthly price : $ <?php the_field('monthly_price'); ?></li>
<li><?php the_field('company_location'); ?></li>
</ul>
<p><?php the_content(); ?></p>
</div><!-- .entry-content -->
</article><!-- #post-## -->
Single page code (I know it’s not the right way to call the plan_type):
<?php
get_header(); ?>
<section id="primary" class="content-area col-sm-12 col-lg-8">
<div id="main" class="site-main" role="main">
<?php
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content-plan');
the_post_navigation();
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop.
?>
</div><!-- #main -->
</section><!-- #primary -->
<?php
get_footer();
As I said a while back, you cannot use the_field()
with a taxonomy field.
<li>Plan type : <?php the_field('plan_type'); ?></li>
and the documentation explains how to use a taxonomy field based on the return value you have set.
What is your return value and what information about the term are you trying to show?
Just trying to show the taxonomy category name that is selected in the custom post entry.
$plan_types = get_field('plan_type');
if ($plan_types) {
?><li>Plan type : <?php echo $plan_types[0]->name; ?></li><?php
}
You must be logged in to reply to this topic.
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.