Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Custom taxonomy Text

  • 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;
    }
  • Hi there,

    Does everything seem to be functioning as expected?

  • It does not, because it’s not appearing in my display.

  • 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?

  • I have a ACF type text named state title and wanted to call it in the Elements Dynamic Data.
    https://share.zight.com/rRuBxNqA

    Element 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

  • 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 );
  • 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 );
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.