t3x
t3x

Reputation: 27

Post name and custom fields in Wordpress Shortcode

I'm using shortcodes in my Wordpress page to display a responsive Google Map centered at specific location. To do it I'm using shortcode supported by external Wordpress plugin. Such shortcode is let's say [map address="New York"]. As all my Wordpress posts titles are names of the cities, I would like to automatize the process and be able to display the titles of the post in place of the address parameter in [map] shortcode. Is it possible? Is there any way to do it? As far as I know nesting code or variables in shortcode isn't supported by Wordpress but maybe there is some kind of a workaround.

The same behavior I'd like to accomplish for 'custom field' - really similar situation.

I'd appreciate any help and any advice

Upvotes: 0

Views: 477

Answers (1)

amedv
amedv

Reputation: 388

Try to put this code to functions.php file in your theme:

function mymap_shortcode() {
    echo do_shortcode('[map address="'.esc_html( get_the_title() ).'"]');
}
function mymap_shortcodes_init() {
    add_shortcode('mymap', 'mymap_shortcode');
}
add_action('init', 'mymap_shortcodes_init');

And then in posts/pages (or even put it directly to template, or create another filter to add that) use shortcode [mymap] that will run [map] shortcode with current page title as parameter.

Upvotes: 1

Related Questions