How to display Single Image?
Support › MB Term Meta › How to display Single Image?Resolved
- This topic has 4 replies, 2 voices, and was last updated 2 years, 9 months ago by
Developer.
-
AuthorPosts
-
February 16, 2023 at 9:41 AM #40548
Developer
ParticipantI have created a shortcode to display a Single Image custom field, however, it does not work.
Here is my code:
function shortcode_function(){ $image = rwmb_meta( 'custom_field_item', ['size' => 'thumbnail'] ); return '<img src="' . $image['url'] . '">'; } add_shortcode('my-shortcode', 'shortcode_function');I am trying to follow the example below but not having any luck.
https://docs.metabox.io/fields/single-image/#template-usage
Any ideas?
February 16, 2023 at 10:31 PM #40563Peter
ModeratorHello,
If the single image is a term meta field, you also need to add the setting
'object_type' => 'term'to the second parameter and add the term ID to the third parameter to output the image.For example:
rwmb_meta( 'custom_field_item', ['size' => 'thumbnail', 'object_type' => 'term'], 123 )where 123 is the term ID.
Please read more on the documentation https://docs.metabox.io/extensions/mb-term-meta/#getting-field-value
February 16, 2023 at 11:42 PM #40567Developer
ParticipantHey Peter,
Thank you for your response.
The custom field,
custom_field_item, that I am using is a Single Photo field on a page with the post ID of 39.I am attempting to display the image saved to the custom field on a different page on the site.
I know how to find a post ID by hovering over a page in the WP admin and viewing the ID as part of the URL, but I am unsure how to find the term ID of the custom field. Are they one in the same? Is it different? This seems to be where I'm getting tripped up.
February 17, 2023 at 10:34 PM #40579Peter
ModeratorHello,
It looks like a WordPress basic question. You can edit a term and get the term ID in the URL, parameter tag_ID. For example, the term ID in this case is 2.
wp-admin/term.php?taxonomy=category&tag_ID=2&post_type=postAlso, please let me know the custom field here is assigned to what object. Term or post?
February 17, 2023 at 11:14 PM #40583Developer
ParticipantPeter,
I got it figured out. The custom field group I am using is for a Post Type, not a Taxonomy, so I believe that term ID doesn't actually come into play. This was obviously my misunderstanding.
Here is the working snippet if it helps anyone in the future. I simply needed to add the post ID to my initial code, where post ID is 123 in this example:
function shortcode_function(){ $image = rwmb_meta( 'custom_field_item', ['size' => 'thumbnail'], 123 ); return '<img src="' . $image['url'] . '">'; } add_shortcode('my-shortcode', 'shortcode_function'); -
AuthorPosts
- You must be logged in to reply to this topic.