lazy-blocks icon indicating copy to clipboard operation
lazy-blocks copied to clipboard

How to get Meta value for Shortcode?

Open bissy opened this issue 2 years ago • 1 comments

Issue description:

I want to make shortcode using Image block's Meta. I save Meta to my Custom post type as Custom fields. ref: https://www.lazyblocks.com/docs/examples/display-custom-fields-meta/

I wrote some code for shortcode in functions.php But get_lzb_meta couldn't get any value, it was return NULL.

How should I do?

// create shortcode [logo]
function short_code_brand_logo() { 
	$image_attr = get_lzb_meta('brand-logo');

	var_dump($image_attr); // NULL

	$logo_meta = '<img src="'.$image_attr['image']['url'].'" alt="'.$image_attr['alt'].'">';
	return $logo_meta;
}
add_shortcode('logo', 'short_code_brand_logo');

My Custom fields value is below:

%7B%22alt%22:%22%22,%22title%22:%22%22,%22caption%22:%22%22,%22description%22:%22%22,%22id%22:471,%22link%22:%22http://example.local/brand/467/281f5767b9337dbda5eaabc9cf401fdc/%22,%22url%22:%22http://example.local/wp-content/uploads/2023/02/281f5767b9337dbda5eaabc9cf401fdc.jpg%22,%22sizes%22:%7B%22thumbnail%22:%7B%22height%22:150,%22width%22:126,%22url%22:%22http://example.local/wp-content/uploads/2023/02/281f5767b9337dbda5eaabc9cf401fdc-126x150.jpg%22,%22orientation%22:%22portrait%22%7D,%22full%22:%7B%22url%22:%22http://example.local/wp-content/uploads/2023/02/281f5767b9337dbda5eaabc9cf401fdc.jpg%22,%22height%22:187,%22width%22:126,%22orientation%22:%22portrait%22%7D%7D%7D

Lazy Blocks Version:

3.3.0

WordPress Version:

6.2.1

Any helpful information to reproduce the issue (screenshots, code parts)

bissy avatar Feb 16 '23 11:02 bissy

Hey.

Can you show me all steps of creating the block with meta, creating a shortcode, and inserting it into your post? I need this to see what exactly is not working correctly, because locally it is working just fine.

Editor view: Shot 2023-02-26 at 17 08 02

Frontend view: Shot 2023-02-26 at 17 10 42

Code:

function short_code_brand_logo() {
    if ( ! function_exists('get_lzb_meta') ) {
        return '';
    }

    $logo_data = get_lzb_meta( 'brand-logo' );

    if ( ! $logo_data ) {
        return '';
    }

    return '<img src="' . $logo_data['url'] . '" alt="' . $logo_data['alt'] . '">';
}
add_shortcode('logo', 'short_code_brand_logo');

nk-o avatar Feb 26 '23 14:02 nk-o