Not working on custom taxonomy page (ACF)

[ad_1]

Hi @adrian2k7, This block does not support term meta field by default, but you can add custom code to make it work with it or any kind of meta fields. Please refer to the plugin description for more details. Here is the sample code I copy from the plugin description:

// Create a function to build the value for the field.
function yourprefix_get_attr_value( $term_id ) {
  $cat_attr_value = get_term_meta( $term_id, 'cat_attr', true );

  // If the meta field is an ACF Field. The code will be:
  // $cat_attr_value = get_field( 'cat_attr', 'term_' . $term_id );

  return $cat_attr_value;
}

// Render the block on the front end.
add_filter( 'meta_field_block_get_block_content', function ( $block_content, $attributes, $block, $post_id ) {
  $field_name = $attributes['fieldName'] ?? '';

  // Replace unique_name_for_cat_attr with your unique name.
  if ( 'unique_name_for_cat_attr' === $field_name && is_tax( 'product_cat' ) ) {
    $term_id = get_queried_object_id();
    $block_content = yourprefix_get_attr_value( $term_id );
  }

  return $block_content;
}, 10, 4);

Thread Starter
Adrian

(@adrian2k7)

Hello Phi,

Thank you.

Then this is a feature request, would be much easier to not write custom code 😉

Adrian

Thank you @adrian2k7 for your suggestion. It has been on my todo list for a future version.

Regards,

Phi

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer