Skip Navigation

[Resolved] Display Toolset custom image field in Gridbuilder WP

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 1 reply, has 2 voices.

Last updated by Minesh 2 years ago.

Assisted by: Minesh.

Author
Posts
#2480179

Ian
backend.jpeg
frontend.jpeg
frontend-expected.jpg

I am trying to add a Toolset custom image field to Gridbuilder WP so I can display it on a card.

Here is the page with the documentation I am trying to use: hidden link The example is: Image block#

I have modified the code to show my custom field "vpcf-vendor-profile-image" and my custom post type of "vendors".

function register_vendor_profile_image_block( $blocks ) {

	// "vendor_profile_image_block" corresponds to the block slug.
	$blocks['vendor_profile_image_block'] = [
		'name'            => 'Vendor Profile Image',
		'render_callback' => 'render_vendor_profile_image_block',
	];

	return $blocks;

}
add_filter( 'wp_grid_builder/blocks', 'register_vendor_profile_image_block' );

function render_vendor_profile_image_block() {

	// Object can be a post, term or user.
	$object = wpgb_get_object();

	// If this is not a post (you may change this condition for user or term).
	if ( ! isset( $object->vendors ) ) {
		return;
	}

	// You have to change "vpcf-vendor-profile-image" by yours.
	$image_id = get_post_meta( $object->ID, 'vpcf-vendor-profile-image', true );

	if ( empty( $image_id ) ) {
		return;
	}

	// You can change the image size "medium_large" to suit your needs.
	$image_url = wp_get_attachment_image_url( $image_id, 'medium_large' );

	if ( empty( $image_url ) ) {
		return;
	}

	printf(
		'<img src="%s" alt="%s" width="100%%" height="400px" style="object-fit:contain">',
		esc_url( $image_url ),
		esc_attr( get_post_meta( $image_id, '_wp_attachment_image_alt', true ) )
	);
}

I do not appear to be getting any results.

#2480487

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

The prefix for the custom field created using Types is "wpcf-".

I see you are fetching the custom image field value using the following line:

$image_id = get_post_meta( $object->ID, 'vpcf-vendor-profile-image', true );

so if you have created the image custom filed with slug "vendor-profile-image" using Toolset Types then you can fetch the image field value (its stored as image URL) to postmeta table as:

$image_id = get_post_meta( $object->ID, 'wpcf-vendor-profile-image', true );

As you noticed that we used "wpcf-" prefix.

Can you please try that and let me know how it goes.