-
zewebzeweb
Hello,
This topic is similar on how to implement the custom taxonomy image, last topic i created is for the images and i wanted to apply it also in texts.
Here is the example code i tried to apply it and change somethings
add_filter('generateblocks_dynamic_image_fallback', 'custom_dynamic_image_fallback',10,3); function custom_dynamic_image_fallback( $fallback_id, $attributes, $block ) { // Check if the block has a class of 'tax-image' if ( isset( $attributes['className'] ) && strpos( $attributes['className'], 'has-taxonomy-image' ) !== false ) { $term = get_queried_object(); $image = get_field('state_image', $term); if ( $image ) { // return the $image return $image; } } return $fallback_id; } add_filter('generateblocks_dynamic_text_fallback', 'custom_dynamic_cat_name_fallback',10,3); function custom_dynamic_cat_name_fallback( $fallback_id, $attributes, $block ) { // Check if the block has a class of 'tax-image' if ( isset( $attributes['className'] ) && strpos( $attributes['className'], 'has-taxonomy-name' ) !== false ) { $term = get_queried_object(); $acfname = get_field('acf_name', $term); if ( $acfname ) { // return the $acfname return $acfname; } } return $fallback_id; }
-
Alvind
Hi there,
Does everything seem to be functioning as expected?
-
zewebzeweb
It does not, because it’s not appearing in my display.
-
Alvind
The second filter you used,
generateblocks_dynamic_text_fallback
, does not exist. Could you explain the whole implementation and where this dynamic text is used, if possible? -
zewebzeweb
I have a ACF type text named state title and wanted to call it in the Elements Dynamic Data.
https://share.zight.com/rRuBxNqAElement type – Block loop template.
When i tried to input the Dynamic data of state title, it shows empty in the actual page. -
Hi there,
there is no hook called:
generateblocks_dynamic_text_fallback
Instead you would use
render_block
add_filter( 'render_block', function( $block_content, $block ) { $target_class = 'your-css-classname'; $acf_field = 'your-acf-option-field'; if ( ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], $target_class ) !== false ) { $queried_object = get_queried_object(); $term_id = $queried_object->term_id; $new_content = get_field( $acf_field , $term_id); if( $new_content ) { return $new_content; } } return $block_content; }, 10, 2 );
In the
$target_class = 'your-css-classname';
– set the class name eg.my-term-title
And that class you add to the blocks > Advanced > Additional CSS Classes field, so the code can find it.In the
$acf_field = 'your-acf-option-field';
you set the field name you want to retrieve from your category options -
zewebzeweb
Hello,
It does print the output but not inside the <h1> tag.
It creates a different <div> but not inside the classname where it should be.add_filter( 'render_block', function( $block_content, $block ) { $target_class = 'cosmetic-h1-title'; $acf_field = 'h1_title'; if ( ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], $target_class ) !== false ) { $queried_object = get_queried_object(); $term_id = $queried_object->term_id; $new_content = get_field( $acf_field , 'term_'.$term_id); if( $new_content ) { return $new_content; } } return $block_content; }, 10, 2 );
-
Alvind
Try this one:
add_filter( 'render_block', function( $block_content, $block ) { $target_class = 'cosmetic-h1-title'; $acf_field = 'h1_title'; if ( ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], $target_class ) !== false ) { $queried_object = get_queried_object(); $term_id = $queried_object->term_id; $new_content = get_field( $acf_field , 'term_'.$term_id); if( $new_content ) { return '<h1>' . $new_content . '</h1>'; } } return $block_content; }, 10, 2 );
- You must be logged in to reply to this topic.