Support

Account

Home Forums General Issues WooCommerce Display Product Category field in the loop

Solved

WooCommerce Display Product Category field in the loop

  • Hello

    I am trying to display a custom field in the product category loop.

    I have this code which displays the box for the field in the correct place, but not the field itself

    It displays the field in the actual category instead, which I don’t want.

    // Category Extra Title Description in Archives
    add_action( ‘woocommerce_after_subcategory_title’, ‘custom_add_product_extratitle’, 12);

    function custom_add_product_extratitle () {

    $term = get_field(‘alternative_title’);
    $othertitle = get_field(‘alternative_title’, $term->taxonomy . ‘_’ . $term->term_id);

    // retrieve the existing value(s) for this meta field.

    if ( is_product_category() ) {

    echo ‘<div class=”extratext”><h2>’.$othertitle.'</h2></div>’;
    }

    }

    https://dizzydev9.co.uk/product-category/light-engines/

  • This will get the field from the current post and not a term, is this where the tax field is located?

    
    $term = get_field('alternative_title');
    

    I’m assuming yes since you’re using code that indicates you know you need to supply a different $post_id in the next line.

    Try this

    
    $othertitle = get_field('alternative_title', 'term_'.$term->term_id);
    
  • Almost, it put me in the right direction

    // Category Extra Title Description in Archives
    add_action( ‘woocommerce_after_subcategory_title’, ‘custom_add_product_extratitle’, 12);

    function custom_add_product_extratitle ($category) {
    $cat_id = $category->term_id;
    $prod_term = get_term($cat_id,’product_cat’);
    $term = get_field(‘alternative_title’);
    $othertitle = get_field(‘alternative_title’, ‘term_’.$prod_term->term_id);

    // retrieve the existing value(s) for this meta field.

    echo ‘<div class=”extratext”><h2>’.$othertitle.'</h2></div>’;

    }

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

The topic ‘WooCommerce Display Product Category field in the loop’ is closed to new replies.