Hello
I’m having trouble outputting the values from a taxonomy within an archive page.
WP set up
CPT called “members”. Taxonomy linked to this called “jobs”.
ACF set up
http://cl.ly/1X0u1o1U0H0x
http://cl.ly/1F1Z2h2f2v3G
Template archive-members.php
while ( have_posts() ) : the_post();
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$job_title = get_field('member_person_job', $taxonomy . '_' . $term_id);
echo $job_title;
echo '<div style="background: #000;"><pre>';
var_dump( $job_title );
echo '</pre></div>';
endwhile;
Browser errors + var dump
Just getting NULL from the var dump and the PHP warnings, with no taxonomy values being output.
Any ideas?
Thanks
Dave
What are the location rules for the field group? Is it a taxonomy term or a post type?
If it’s a post type you shouldn’t be using the taxonomy and term id for post id, and since in inside the loop you don’t need the post id at all.
What you’ll get for output depends on what you have the field set to return.
while ( have_posts() ) : the_post();
$job_title = get_field('member_person_job');
echo $job_title;
echo '<div style="background: #000;"><pre>';
var_dump( $job_title );
echo '</pre></div>';
endwhile;
OK that makes some sense.
So if I have it set to return the ID, how do I then get the term, currently it outputs the ID number via the echo, so presumably I’d need something like:
echo $job_title->term_id;
But then I’m back to using this block:
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
Which produces the errors.
What you have there won’t work, the queried object is a list of posts, unless you are in the template to show that particular taxonomy. Instead you should use get_term() https://codex.wordpress.org/Function_Reference/get_term
`
$job_term = get_term(get_field(‘member_person_job’), ‘taxonomy-name’));
‘
Thank you.
Using this:
$job_term = get_term(get_field('member_person_job'), 'taxonomy-name');
echo $job_term;
Produces this error:
However, I can get the term using this:
$job_term = get_the_term_list( $post->ID, 'jobs', '', ',' ); $terms = strip_tags( $terms );
Based on your comment, the original block used on taxonomy-jobs.php should work, but this still results in NULL:
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$job_title = get_field('member_person_job', $taxonomy . '_' . $term_id);
echo $job_title;
By all accounts this should work on the taxonomy page?
The first one creates an error because the taxonomy “taxonomy-name” does not exist. I didn’t know the name of the taxonomy and you needed to replace that with the name of your taxonomy
$job_term = get_term(get_field('member_person_job'), 'jobs');
The last block of code will not work. The currently queried object is probably not a taxonomy. The only way that will work would be on a url that looks something like http://www.yourside.com/jobs/JOB_NAME_HERE
Thank you.
I’ve got this into a position I need, so thanks, but for the sake of this thread being useful, I should say that this:
$job_term = get_term(get_field('member_person_job'), 'jobs');
echo $job_term;
Results in this error on the archive page:
That’s because you’re trying to echo an class object.
$job_term = get_term(get_field('member_person_job'), 'jobs');
print_r($job_term);
will show the values of the object
The topic ‘Get values from Taxonomy terms on archive.php page = NULL’ 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.