Support

Account

Home Forums Backend Issues (wp-admin) Conditional branch to "Preview Size" on edit screen in diffrent custon post type

Solved

Conditional branch to "Preview Size" on edit screen in diffrent custon post type

  • Hello.

    “Preview Size” has only one option for Selectbox ( Field Type “Image” ).

    Could I specify different values for each Custom Post Type ?

    For example:

    When I edit on Custom Post Type A, an image is sized to “preview_for_A (320 x 240)”.
    When I edit on Custom Post Type B, an image is sized to “preview_for_B (100 x 100)”.

    * preview_for_A and preview_for_B are defined by add_image_size in funtions.php

    If it is not possible, I do not share single custom field (Field Type “Image”) by 2 Custom Post Types.

  • You can use an acf/prepare_field filter to alter any setting of a field.

    To see setting of a field to know what you need to change use the ACF export to PHP tool so that you can see the structure of the image field.

    You would need to conditionally change the right setting based on the post type being edited. I don’t have any code handy for hour you would do this. You may need to look at the values in $_GET when adding a new post.

  • Dear John Huebner.

    Thank you for your quick reply.

    It works fine as below.

    in functions.php

    function my_acf_modify_img_field($field) {
    	if(get_post_type() === 'Post Type A') {
    		$field['preview_size'] = 'preview_for_A';
    	} elseif(get_post_type() === 'Post Type B') {
    		$field['preview_size'] = 'preview_for_B';
    	}
    	return $field;
    }
    
    add_filter('acf/prepare_field/name=cf_my_img', 'my_acf_modify_img_field');

    I deeply appreciate you.

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

You must be logged in to reply to this topic.