0

I have loop that displays only the direct children of a term on a taxonomy page. I also have image field, attached to custom taxonomy. Now, I want to display image in my page template:

<?php
  $current_term_id = get_queried_object_id();
    $taxonomyName = "car-brand";
    //Could use ACF or basic custom field to get the "parent tax ID" dynamically from a page. At least that's what I would do.
    $parent_tax_ID = $current_term_id;
    $parent_tax = get_term($parent_tax_ID);

    echo '<ul>';
    $terms = get_terms( $taxonomyName, array( 'parent' => $parent_tax_ID, 'orderby' => 'slug', 'hide_empty' => false ) );
    foreach ( $terms as $term ) {
        echo '
        <div class="am-ts-item">
          <a href="' . get_term_link( $term ) . '">
              <div class="am-ts-img">
              <img src=""> 
                  <span>2004 - 2007</span>
              </div>
              <div class="am-ts-info">
                  <h2>' . $term->name . '</h2>
              </div>
          </a>
        </div>
        ';
    }
    echo '</ul>';
?>

My image field: "am-brand-img"

2
  • Can you please add details how are you adding image to taxonomy? Jul 17, 2023 at 10:51
  • @Pratikbhatt I crated a new ACF group and I added role to brand taxonomy. Now I have a image custom filed in my taxonomy edit page.
    – och
    Jul 17, 2023 at 10:59

2 Answers 2

0

I found a solution for this:

<?php
    $current_term_id = get_queried_object_id();
    $taxonomyName = "car-brand";
    $parent_tax_ID = $current_term_id;
    $parent_tax = get_term($parent_tax_ID);
    $terms = get_terms( $taxonomyName, array( 'parent' => $parent_tax_ID, 'orderby' => 'slug', 'hide_empty' => false ) );
    foreach ( $terms as $term ) {
      $image2 = get_field('am-brand-img', $term);
        echo '
        <div class="am-ts-item">
          <a href="' . get_term_link( $term ) . '">
              <div class="am-ts-img">
              <img src="'.$image2['url'].'" alt="'.$image2['alt'].'" />
                  <span>2004 - 2007</span>
              </div>
              <div class="am-ts-info">
                  <h2>' . $term->name . '</h2>
              </div>
          </a>
        </div>
        ';
    }
?>
0

Get the ACF value from a custom taxonomy.

When retrieving custom fields from taxonomy instead of directly supplying the term ID, you need to concatenate the taxonomy name and the term ID separated by underscore; i.e. car-brand_4

$taxonomy_id = "car-brand_4"; // 'car-brand' (custom taxonomy) sample term ID = 4

$image = get_field( 'am-brand-img', $taxonomy_id );

Get a value from different objects

This example shows a variety of $post_id values to get a value from a post, user, term and option.

$post_id = false; // current post
$post_id = 1; // post ID = 1
$post_id = "user_2"; // user ID = 2
$post_id = "category_3"; // category term ID = 3
$post_id = "event_4"; // event (custom taxonomy) term ID = 4
$post_id = "option"; // options page
$post_id = "options"; // same as above

$value = get_field( 'my_field', $post_id );

See get_field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.