Show image custom field as thumbnail

Content Views Pro version 5.5.0 and higher

Since Content Views Pro version 5.5.0, you can easily show image custom field as thumbnail without coding.

Here is the configuration in Display Settings >> Fields Settings >> Thumbnail of the View to show any image custom field as thumbnail:

Content Views Pro version 5.4.1 and prior

For previous Pro versions, to show the custom field as thumbnail, please follow steps below:

  • In the View where you want to show the custom field as thumbnail, select the Show Thumbnail checkbox in Display Settings >> Fields settings.
  • Under Display Settings >> Fields settings >> Thumbnail >> Substitute, please configure as below:
    CVP - show custom field as thumbnail
  • Add this code to file functions.php of your active theme:
    // Content Views Pro - Use custom field as thumbnail
    add_filter( 'pt_cv_field_content_excerpt', 'cvp_theme_custom_field_as_thumbnail', 999, 3 );
    function cvp_theme_custom_field_as_thumbnail( $args, $fargs, $post ) {
    	if ( empty( $fargs ) ) {
    		$custom_field = 'CUSTOM_FIELD';
    
    		$fval = null;
    		if ( class_exists( 'CVP_CTF' ) ) {
    			$ctf = new CVP_CTF( $custom_field, $post, true, '' );
    			if ( !empty( $ctf->field_value ) ) {
    				$fval = $ctf->field_value;
    			}
    		}
    
    		if ( empty( $fval ) ) {
    			$meta = get_post_meta( $post->ID );
    			if ( !empty( $meta[ $custom_field ][ 0 ] ) ) {
    				$fval = $meta[ $custom_field ][ 0 ];
    			}
    		}
    
    		if ( $fval ) {
    			$args = sprintf( "<img src='%s'>", $fval );
    		}
    	}
    
    	return $args;
    }
    

    (replace CUSTOM_FIELD with slug/key of your custom field).

Notice: If you created custom field with Toolset plugin, you may need to add prefix wpcf-. For example, you created a custom field post-image with Toolset plugin, then the value of CUSTOM_FIELD should be wpcf-post-image.

Thank you,