简体   繁体   中英

Wordpress Function Within a Shortcode

In brief, I am trying to effectively insert a PHP function into the shortcode and having no luck.

The shortcode:

[res_map address=""]

The function:

<?php the_field('address'); ?>

What I have so far:

<?php echo do_shortcode('[res_map address="' . the_field("address") . '"]'); ?>

Any help on how to properly do this will be highly appreciated.

You may try something like this

$ret = the_field("address");
echo do_shortcode('[res_map address = "'. $ret .'"]');

In this case, your function must return a string, ie

function the_field($arg)
{
    // ... 
    return 'something';
}

Because do_shortcode function expects string as it's argument and it's required. For example, it should be something like this

echo do_shortcode('[res_map address = "something"]');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM