How to display Single Image?

Support MB Term Meta How to display Single Image?Resolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #40548
    DeveloperDeveloper
    Participant

    I 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?

    #40563
    PeterPeter
    Moderator

    Hello,

    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

    #40567
    DeveloperDeveloper
    Participant

    Hey 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.

    #40579
    PeterPeter
    Moderator

    Hello,

    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=post

    Also, please let me know the custom field here is assigned to what object. Term or post?

    #40583
    DeveloperDeveloper
    Participant

    Peter,

    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');
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.