Site logo

Archived topic

Set image 2 in page and archive template - Metabox

1 reply · Started by Prabakaran on January 20, 2022

Viewing posts 1–2 of 2

Hello,

I am using the Metabox plugin to manage custom fields and CPT. I have an image field that is stored in a custom database table. The Metabox plugin has a helper function to retrieve the data from the custom database table.

I want to use the image (except the thumbnail which is stored in the post meta) in the post template and also in the archive.

For example, the thumbnail image is set as image 1. I want to use image 2 in the page template and the archive page.

Note: I have created the page template with 'elements'.

Thanks

Hi there,

This seems quite tricky to do if there's only 1 custom field w/ 2 different image value.

If a post meta has multiple image values on a key, it's going to be an array of images.

This means you basically have to create a condition where the function has to fetch a specific array value depending on a specific condition.

Something like:

$image_array = get_post_meta(get_the_ID(), 'your_image_custom_field', false);

if(condition 1 here) {
$image = $image_array[0]->guid;
} if (condition 2 here) {
$image = $image_array[1]->guid;
}

I'm not sure if guid is the correct array value to fetch the image in your case so you may have to do a var_dump of the custom field's key through get_post_meta() to know what's the write array key to get.

To display this flexibly, I think you may have to write this one in shortcode form.

This archived topic is closed to new replies.