如何创建一个快捷码来打印存储在POST元数组中的特定值?

时间:2017-07-18 作者:Emanuel David Brigham Vitorino

我在通过快捷码打印Visual Composer网格生成器创建的WordPress循环中存储在post meta数组中的特定值时遇到问题(我也尝试了网格生成器自定义字段,但找不到任何示例)。

我尝试在函数中添加此短代码。php:

function dmc_prezzo()
{
$postid = get_the_ID(); // get id from post in loop
$meta_data = get_post_meta($postid,\'listing_options\'); // select array
return $meta_data[\'price:value\'] . \' \' . $meta_data[\'price:original\']; // extract keys
}
add_shortcode(\'dmcprezzo\', \'dmc_prezzo\');  //usage [dmcprezzo]
然后,我在VC网格模板的文本字段中添加了短代码but it is outputting nothing!

以下是使用DPM插件从帖子中提取的目标帖子元数组:

    listing_options: s:349:"a:8:{s:5:"price";a:2:{s:5:"value";s:5:"17300";s:8:"original";s:5:"19800";}s:17:"custom_tax_inside";s:0:"";s:15:"custom_tax_page";s:0:"";s:8:"city_mpg";a:1:{s:5:"value";s:3:"5.7";}s:11:"highway_mpg";a:1:{s:5:"value";s:3:"4.6";}s:12:"custom_badge";s:7:"Venduta";s:5:"video";s:43:"https://www.youtube.com/watch?v=IBCOAumh6aI";s:10:"short_desc";s:0:"";}";
我可以看到包含数组的变量被调用$listing_options 但我不知道如何正确设置代码。

我感兴趣的值是数组开头的price:value和price:original:

s:349:"a:8:{s:5:"price";a:2:{s:5:"value";s:5:"17300";s:8:"original";s:5:"19800";}
有人能帮忙吗?

1 个回复
最合适的回答,由SO网友:Johansson 整理而成

这是未序列化时阵列的输出:

Array
(
    [0] => Array
        (
            [price] => Array
                (
                    [value] => 17300
                    [original] => 19800
                )

            [custom_tax_inside] => 
            [custom_tax_page] => 
            [city_mpg] => Array
                (
                    [value] => 5.7
                )

            [highway_mpg] => Array
                (
                    [value] => 4.6
                )

            [custom_badge] => Venduta
            [video] => https://www.youtube.com/watch?v=IBCOAumh6aI
            [short_desc] => 
        )

)
因此,要获取价格,您必须将您的短代码更改为:

function dmc_prezzo() {
    $postid = get_the_ID(); // get id from post in loop
    $meta_data = get_post_meta($postid,\'listing_options\'); // select array
    return $meta_data[\'listing_options\'][\'price\'][\'value\'] . \' \' . $meta_data[\'listing_options\'][\'price\'][\'original\']; // extract keys
}
add_shortcode(\'dmcprezzo\', \'dmc_prezzo\');  //usage [dmcprezzo]
如果您已经发布了整个序列化数据就更好了,我不确定它是否是嵌套的。

结束

相关推荐

shortcode get thumbnail size

如何获取短代码缩略图大小?我的代码在函数中获取短代码缩略图大小:function thumb_medium( $atts, $content = null ) { return wp_get_attachment_url( get_post_thumbnail_id( $post_id, \'medium\') ); //or wp_get_attachment_url( get_post_thumbnail_id( $post_id, \'large\') ); //or