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 ) )
);
}
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: