Show image custom field as thumbnail

With Content Views Pro, you can display WordPress featured image, or image inside post content as thumbnail of post easily without coding.
If you have an image custom field, and want to show the custom field as thumbnail, please follow steps below:

  • In the View where you want to show the custom field as thumbnail, 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,