Support

Account

Forum Replies Created

  • @benconnective remember to add: ‘mode’ => ‘preview’ in attibutes.

  • @nikolaimoskito did you try an absolute path to the image?

    “example”: {
    “attributes”: {
    “data”: {
    “preview_image_help” : “https://domain.com/wp-content/themes/your-theme/any-folder/accordion/hallo.jpg”
    }
    }
    }

  • @rrmeines if your preview image is a small image (maybe low res) you wont have any problem with breaking the editor, I use it in dozens of sites (one has 53 ACF blocks) without problems. Sure a native solution from ACF is more than welcome.

  • Hi everyone,
    After reading a zillion of posts on all over internet about how to add an image as preview in custom ACF Blocks and getting nowhere I found myself a solution:

    1) You need to add ‘example’ when you register your ACF block, add “mode” attribute as “preview” and any specific data you want to pass.

        acf_register_block_type(
        	array(
    	        'name' => 'name-of-block',
    	        'title' => __('Your Title'),
    	        'description' => __('Your description'),
    	        'post_types' => array('page'),
    	        'category' => 'common',
    	        'render_template' => '../your/path/block_template.php', // your template
    	        'icon' => '', // svg code for icon
    	        'keywords' => array('Keyword', 'another'),
    	        'example'  => array(
    	            'attributes' => array(
    	                'mode' => 'preview',
    	                'data' => array(
    	                	'preview_image_help' => '../your/dir/block-image-preview.jpg',
    	                )
    	            )
    	        )
    	    )
    	);
    

    WARNING:
    The field “preview_image_help” needs to be a unique name, and CAN NOT EXIST in the block fields.

    The block will be render in preview mode in 2 places, the editor and the block inserter preview, (when you hover on the name of the block).
    When renders in the editor it will have all the fields you define in the block.
    When renders in the inserter preview will have only the field “preview_image_help”.

    2) Now the render part(in your template), check for the unique field and you will know where the render is taking place:

    	 <?       
    	    if( isset( $block['data']['preview_image_help'] )  ) :    /* rendering in inserter preview  */
    
    			echo '<img src="'. $block['data']['preview_image_help'] .'" style="width:100%; height:auto;">';
    	
    		
    		else : /* rendering in editor body */
    			
    			$text   = get_field('text') ?: 'Default text ...';
    			$author = get_field('author') ?: 'Author name';
    			$title  = get_field('title');
    			?>
    			<div class="my_class">
    				<span class="class-title"><?php echo $title; ?></span>
    				<span class="class-text"><?php echo $text; ?></span>
    				<span class="class-author"><?php echo $author; ?></span>
    			</div>
    			<?
    			
    		endif;
    

    Hope this helps!

  • We tried lots of solutions here/there/anywhere and they didn’t work for us, we have a series of complex flex content fields and lots of custom post-types, not all layouts need to be used in all post types. We come out with this, maybe help someone:

    The basic idea is to hide the layout from the acf popup.
    This hides 3 layouts from the ACF Popup when you click ADD ROW in POSTS.

    
    function acf_flex_layouts_filter() {
    $type = get_post_type();
    if ($type=="post"): 
    ?>	
    <style>
       .acf-fc-popup a[data-layout='layout_name_1'] { display: none; };
       .acf-fc-popup a[data-layout='layout_name_3'] { display: none; };
       .acf-fc-popup a[data-layout='layout_name_8'] { display: none; };
    <style>
    <? 
    endif;
    }
    add_action('admin_head', 'acf_flex_layouts_filter');
    

    You adapt it to your needs.
    We created arrays with Layouts to hide for every post types:

    
    $hide_laypout['post'] = array( 'lay1','lay2','lay3'...);
    $hide_laypout['page'] = array( 'lay8','lay9' ...);
    $hide_laypout['movie'] = array( 'lay7','lay3' ...);
    etc etc...
    

    And loop then inside <style> </style>.

    Not sure if is the best solution but works for us.

  • We tried lots of solutions here/there/anywhere and they didn’t work for us, we have a series of complex flex content fields and lots of custom post-types, not all layouts need to be used in all post types. We come out with this, maybe help someone:

    The basic idea is to hide the layout from the acf popup.
    This hides 3 layouts from the ACF Popup when you click ADD ROW in POSTS.

    
    function acf_flex_layouts_filter() {
    $type = get_post_type();
    if ($type=="post"): 
    ?>	
    <style>
       .acf-fc-popup a[data-layout='layout_name_1'] { display: none; };
       .acf-fc-popup a[data-layout='layout_name_3'] { display: none; };
       .acf-fc-popup a[data-layout='layout_name_8'] { display: none; };
    <style>
    <? 
    endif;
    }
    add_action('admin_head', 'acf_flex_layouts_filter');
    

    You adapt it to your needs.
    We created arrays with Layouts to hide for every post types:

    
    $hide_laypout['post'] = array( 'lay1','lay2,'lay3'...);
    $hide_laypout['page'] = array( 'lay8','lay9' ...);
    $hide_laypout['movie'] = array( 'lay7','lay3' ...);
    etc etc ...
    

    And loop then inside <style> </style>.

    Not sure if is the best solution but works for us.

  • Not sure if this help someone:

    In my case “update_sub_field()” fails to update POST_META table:
    It creates an additional record for the post without repeater name:

    _0_subfield
    _1_subfield
    _2_subfield

    and does’nt update the good records:

    repeaterName_0_subfield
    repeaterName_1_subfield
    repeaterName_2_subfield

    And from them it updates the wrong ones, “update_sub_field()” seems like the function fails to “add” repeater name to the meta_key, anyway I found a workaround:

    Use the core function “update_post_meta()” :

    update_post_meta( $post_id, “repeaterName_0_subfield”, “new value” );
    update_post_meta( $post_id, “repeaterName_1_subfield”, “new value” );
    update_post_meta( $post_id, “repeaterName_2_subfield”, “new value” );

    This function update values in custom fields in Post_Meta table.
    PD: If you need to know how many “rows” has your repeater you can use this:

    $number_rows = get_post_meta( $post_id, “repeaterName” );

    This works for me perfectly, I spend a lot of time struggling with update_sub_fiel(), hope there will be an update it soon.

  • I have same issue I need to update a sub_field value based in another subfield and I tried everything and is not working, row loop is fine but it did not update the subfield no matter the way i try ( name_fiel, array(repeater), key_field) nothing works.

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