Support

Account

Home Forums Front-end Issues Returning Custom Field Values (including Taxonomy Terms) on Term archive pages

Solved

Returning Custom Field Values (including Taxonomy Terms) on Term archive pages

  • Having a bit of a headache displaying custom fields on term archive pages; values are not been returned.
    Problem started with not been able to return custom field values that are terms; however it seems like I just cant return any custom field value on a term archive page in general.

    I decided to troubleshoot why no values were been returned/displayed; I enabled the custom field for post types as well.
    Created text custom field ‘test_field‘. Test field was populated for relevant post types and taxonomy term.

    As per ACF documentation, below coding alone should be enough to display field value on either post or custom post type or term (taxonomy) archive page:

    <?php
    the_field('test_field');
    ?>

    But no value shows up on a current term archive page.
    Neither does any value show up on current term archive page when executing:

    <?php
    $value = get_field('test_field');
    the_field('value');
    ?>

    So I approached my objective a different way.

    <?php
    $queried_object = get_queried_object();
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;
    
    $value = get_field( 'test_field', $term_id);
    if( $value ) {
        echo $value;
    } else {
        echo 'empty';
    }
    ?>

    Again, values are only displayed on post type pages; I see empty been returned on the term page. What I’m I doing wrong? How do I get custom field values to be displayed on term archive pages?

    Further more, What is the best way to return/display values that are taxonomy terms (array); avoiding errors like “Catchable fatal error: Object of class WP_Term could not be converted to string“? (A single value vs. multiple values?) (Linkable term values vs. non linkable string values?).

  • I’m a little confused about where these fields are located. Are they located on a post or on a term? What are the location rules?

  • @hube2: Hello John, Thanks for taking a look

    As titled ‘Returning Custom Field Values on Term archive pages’… I decided to troubleshoot why no values were been returned/displayed; I enabled the custom field for post types as well.

    How do I get custom field values to be displayed on term archive pages?

    Hence the objective here is custom field ‘test_field‘ was created with a term location. I later also included locations: post type post & post type custom post‘ as a means of testing.

    Results seem to be returned ok for post types.

    Hence the questions were:

    1 – How do I get custom field values to be displayed on term archive pages? This is my main problem.
    2 – Suggestions on the best way to return/display values that are taxonomy terms (array); avoiding errors like “Catchable fatal error: Object of class WP_Term could not be converted to string“?
    3 – How can you return these terms if it was a single term vs. multiple and if needed as linkable or non linkable string returned?

    @hube2: Hope I have addressed your confusion.

  • This was almost right

    
    <?php
    $queried_object = get_queried_object();
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;
    
    $value = get_field( 'test_field', $taxonomy.'_'.$term_id);
    if( $value ) {
        echo $value;
    } else {
        echo 'empty';
    }
    ?>
    
  • @hube2: Thanks.. This returns an array; so I believe we getting closer.

  • @hube2: I’m able to now retrieve an array with:

    <?php
    $queried_object = get_queried_object();
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;
    
    $value = get_field( 'test_field', $taxonomy.'_'.$term_id);
    if( $value ) {
        echo $value;
    } else {
        echo 'empty';
    }
    ?>

    Why cant the_field() do what echo does here?

    I retrieve an array when the custom field is text & get an error message when its a taxonomy term:

    “Catchable fatal error: Object of class WP_Term could not be converted to string”

    I also retrieve an array when the custom field is an image.

    Looks like we now have to iterate through the string array in order to show the value; brings back the 2nd aspect of my question.

    How do you best display the returned string(s)/array on a taxonomy term archive page:
    -if ACF field value is text?
    -if ACF field value is an image?
    -if ACF field value is a single or multiple taxonomy terms?
    -if I would like to display the term values as linkable vs non linkable?

  • I guess this is where printf() and sprintf() come into play right? Because the_field() alone doesn’t help with all desired output display formats.

  • How you display the value depends on the value returned. A taxonomy field can return either the term ID, or the term object, or it can return an array of term IDs or term objects depending on the field’s settings. An image field can return one of three different value types. You’ll need to read the documentation on each of the different field types for examples of how to display the values.

  • My main focus has been on
    displaying a taxonomy field that is an object on a taxonomy archive page

    . I keep getting the error message:

    “WP_Term could not be converted to string”

    I’m missing something…

    Note: When taxonomy field is set to return term ID, no error message; ID is successfully returned.

    I did not find any documentation on your site on returning/displaying an object from a taxonomy custom field.

  • A taxonomy field returns either 1 term object or an array of term objects depending on what you set the field for. You need to access the properties of the object.

    The documentation for the taxonomy field is here https://www.advancedcustomfields.com/resources/taxonomy/

  • @hube2
    Thanks for the guidance so far. I’ve managed to solve all my problems, including the term returning concern; just one little obstacle left; might be more php/html related.

    I manage to return the custom field taxonomy terms:

    <?php 
    $queried_object = get_queried_object(); 
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;
    
    $terms = get_field( 'custom_field', $taxonomy.'_'.$term_id);
    
    foreach( $terms as $term ):?>
    <a href="<?php echo get_term_link( $term );?>"> <?php echo $term->name; ?> </a>
    <?php endforeach; 
    ?>

    This does a good job of returning and listing the terms. It lists them down the page (separate lines). I need to list the terms separated by commas (on the same line). There are a few loop suggestions on the net about listing array entries separated by commas including the implode() function. Its proving to be a lil’ tricky incorporating a loop or the implode() function within this foreach() loop and achieving the desired output. It seems the foreach() function is forcing the values out on separate lines? Further more what about listing the terms in order of child first and parent last?

    Would appreciate your directions/suggestions.

  • I don’t see any reason why the values should be listed on different lines, you don’t have any <div>'s or <br />'s in there that would cause this. Perhaps the formatting issue is CSS related? for example if the <a>'s are set to display as blocks or there’s some other CSS formatting that’s causing it.

  • @hube2: All problems solved.
    Thanks for your assistance.

  • I have a custom field with location rule on custom post type and i want to check this custom field value(Radio button field) on Taxonomy terms page .How can do that
    is premium ? Yes No
    this is a field on custom posts ‘listing’ and i want to check this field value on
    ‘taxonomy-listing’ Page .
    I don’t want them to use inside WP Query , But want to run various WP Query according to value of ‘is premium?’

    Pls Help

Viewing 14 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic.