Support

Account

Home Forums General Issues How do you display Advanced Custom Fields in taxonomy-product_cat.php?

Solved

How do you display Advanced Custom Fields in taxonomy-product_cat.php?

  • I want to be able to populate a Advanced Custom Field WYSIWYG just below the Product Category title on the taxonomy-product_cat.php template. For the life of me I can’t figure it out. I can set up and display ACF’s all day long in regular WordPress templates, but woocommerce is a driving me mad. My ACF is set to show on “taxonomy term” and “product cat”. Here is the code I have in my functions.php. The html displays but the actual field will not. PLEASE HELP! THANKS!


    // Add ACF Info to Product display page
    add_action( 'woocommerce_before_shop_loop', "ACF_product_content", 10 );

    function ACF_product_content(){

    echo '<h2> ACF Content </h2>';

    if (function_exists('get_field')){
    echo '<p>Woohoo, the_field function exists! </p>';

    echo the_field('custom_category_content');
    }
    }

  • You have to supply the_field function with the proper ID value when getting fields associated with a taxonomy term. See the end of “usage” on this page http://www.advancedcustomfields.com/resources/functions/the_field/.

    If this function is called on only tax/term templates you can use the following to get the current term id.

    $term_id = get_queried_object()->term_id

    The you combine that with the taxonomy name to get the field value

    $post_id = ‘taxonomy-name_’.$term_id.

  • First of all, thank you for the assistance. Am I correct in thinking that this would get placed into the custom taxonomy-product_cat.php file itself:


    <?php the_field('custom_category_content','clothing'); ?>

    The other question is where do I place the code (show below) that you suggested? In the template file or within the function in the functions file?


    $term_id = get_queried_object()->term_id;
    $post_id = ‘taxonomy-name_’.$term_id;

  • You would put the second part of your code just before the first part

    
    $term_id = get_queried_object()->term_id;
    $post_id = 'product_cat_'.$term_id;
    the_field('custom_category_content', $post_id);
    

    And this should all go into your custom taxonomy template (taxonomy-product_cat.php) where you want custom_category_content to be displayed.

  • It’s still not showing up. I added the code below to my taxonomy template:
    <div>
    <?php
    $term_id = get_queried_object()->term_id;
    $post_id = ‘taxonomy-name_’.$term_id;
    the_field('custom_category_content', $post_id);
    ?>
    </div>

    I also double checked my custom field location. It’s set to: “Taxonmony Term” is equal to “Product Categories”. Also, do I still need the woocommerce “add_action” function (in my functions file)? IF so, is the one I have correct? So sorry for this… thanks again.

  • I’m not sure. Are you sure about the taxonomy name being “product_cat_” I’m assuming it is based on the name of the template file being used.

    and you may need your action and function that you’d normally use for woocommerce. I don’t know a lot about how ACF plays with woocommerce.

    What I do know is that your original code the_field('custom_category_content'); was not working because you were not supplying it with the needed id value to get the value of a custom field that is attached to a term according to the ACF doc (http://www.advancedcustomfields.com/resources/functions/the_field/)

    You can make sure that $term_id = get_queried_object()->term_id; is returning a valid term ID.

    $object = get_queried_object();
    echo $object->term_id;

    you can even check the entire queried object to see what’s in there

    object = get_queried_object();
    print_r($object);
  • Both tests confirmed it was returning the term ID. The first returned 16 and the second returned:

    stdClass Object ( [term_id] => 16 [name] => Clothing [slug] => clothing [term_group] => 0 [term_taxonomy_id] => 16 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 12 [filter] => raw )

    As for woocommerce support on this particular subject, there isn’t much out there at all, and what I’ve found didn’t work. At the start, it seemed like something I could track down because adding a custom WYSIWYG field above the main woocommerce product loop would allow for a simple way to customize the page content for each product category… but nothing. I even posted the same issue on WooCommerce’s support site and still nothing after 3 days. So strange.

  • With HUB2’s kind help this solution finally worked! Thanks a ton HUB2!

    <?php

    $term_id = get_queried_object()->term_id;
    $post_id = 'product_cat_'.$term_id;
    $custom_field = get_field('custom_category_content', $post_id); // My Advanced Custom Field Variable

    ?>

    <div><?php echo $custom_field; ?> </div> <?php // Get Advanced Custom Field Value ?>

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

The topic ‘How do you display Advanced Custom Fields in taxonomy-product_cat.php?’ is closed to new replies.