Home Forums Front-end Issues Can't display a Custom field in parent level taxonomy


Can't display a Custom field in parent level taxonomy

  • I currently have a custom field that I want to add to a taxonomy, in this case a category (product category in woocommerce). I have a parent > child category structure. The custom field holds a colour picker and a value.

    So I create the field on the child and give it the value #666666 for example.

    Then on the taxonomy template, I can call the value with:

    $term = get_queried_object();
    $color = get_field('course_colour', $term);
    echo  $color;

    This works fine if I am viewing the child taxonomy. However, I also need this to display on the parent taxonomy also. Currently a var_dump simply shows its value as NULL when on the parent.

    What do I need to add to get this to work please?

  • you would need to use get_terms() to select children of the current therm. Then if this returns children then you would get the field from the child instead of from the current term.

  • Thanks for your reply. My understanding of PHP is quite basic. Would it be possible to get an example I could try and work with? Ive read the get_terms docs and done some searching, but have been unable to get anything to work.

    // current term
    $current_term = get_queried_object();
    // child terms
    // this returns an array of terms
    $args = array(
      'taxonomy' => 'YOUR TAXONOMY HERE',
      'parent' => $current_term->term_id,
      // you may need other arguments depending on your needs
    $child_terms = get_terms($args);
    // you need to maybe loop through the child terms gotte
    // to pick which one you want to use
    // I'm assuming that you only want to use the first one
    $child_term = false; // set it to false to begin with
                         // we'll use this later
    if ($child_terms) {
      $child_term = $child_terms[0];
    // make a decision
    if ($child_term) {
      // get field value(s) from child term
      $color = get_field('color', $child_term);
    } else {
      // get field value(s) from current term
      $color = get_field('color', $current_term);
    // do something with the values
    echo $color;
  • This stops the woocommerce loop rendering. It seems to break at:

    $child_terms = get_terms($args);

    What i’m trying to achieve is that each sub category has a colour. This is defined with the ACF colour picker. That then creates an inline style which gives each subcategory a border, defines button colours etc etc.

    The $term = get_queried_object(); works fine inside the woocommerce product loop when viewing the sub category and children of it, but not on the parent, which would essentially show a number of sub categories with there own colours defined by the ACF field.

    So this is my current loop thats working on sub categories to help explain better.

    global $product;
    // Ensure visibility
    if ( empty( $product ) || ! $product->is_visible() ) {
    // Colour Picker Setup
    $term = get_queried_object();
    $color = get_field('course_colour', $term); // Get ACF Field
    //echo  $color;
    $remove = substr($color, 0, -1); // RGBA for opacity block over image, remove last )
    $opacity = $remove . '.8)'; // RGBA for opacity block over image, add .5) to straing
    $chop = substr($color, 0, -3); // For inline styles, we need to remove the alpha value
    $border = $chop . ')'; // add closing bracket to inline style
    //echo $opacity;
    <div class="col-md-4">
      <div class="product-img">
    	 * woocommerce_before_shop_loop_item hook.
    	 * @hooked woocommerce_template_loop_product_link_open - 10
    	do_action( 'woocommerce_before_shop_loop_item' );
    	 * woocommerce_before_shop_loop_item_title hook.
    	 * @hooked woocommerce_show_product_loop_sale_flash - 10
    	 * @hooked woocommerce_template_loop_product_thumbnail - 10
    	do_action( 'woocommerce_before_shop_loop_item_title' );
        <div class="overlay" style="background-color: <?php echo $opacity; ?>"></div>
       <div class="product-box" style="color: <?php echo $border; ?> !important; border-style: solid; border-width: 4px; border-color: <?php echo $border; ?>">
    <div class="type" style="background: <?php echo $border; ?> ">AQA/WJEC LEVEL 3</div>
    	 * woocommerce_shop_loop_item_title hook.
    	 * @hooked woocommerce_template_loop_product_title - 10
    	do_action( 'woocommerce_shop_loop_item_title' );
    <div class="row">
    	<div class="col-md-6">
    	 * woocommerce_after_shop_loop_item hook.
    	 * @hooked woocommerce_template_loop_product_link_close - 5
    	 * @hooked woocommerce_template_loop_add_to_cart - 10
    	do_action( 'woocommerce_after_shop_loop_item' );
    	<div class="col-md-6">
    		<?php echo do_shortcode("[ti_wishlists_addtowishlist]"); ?>
  • Sorry I didn’t get back to you sooner. I’m not really sure what it is that WooCommerce (WC) is doing here. I think that it can either be showing a product or a sub category as a “shop_loop_item”

    You’re also saying this is a category page, but the page starts with global $product; which would indicate a product page and not a category page. From what I can tell looking at the code you posted you are editing the file that shows products in a loop content-product.php, WC uses a different template for showing sub categories content-product_cat.php. So I’m not really sure what you doing.

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

The topic ‘Can't display a Custom field in parent level taxonomy’ is closed to new replies.