Search code examples
phpwordpressshortcode

php within php to include wordpress custom field


I am trying to incorporate an image slider shortcode into a custom WordPress theme template, so that the client will be able to paste the shortcode into the custom field and the template will read this and display it correctly. I do not want the client to paste the shortcode in the body of the post, as the slider needs to be displayed outside of the post wrapper (at full browser width).

I don't know much about php, so would really appreciate any help with this!

The code I have so far to display the slider via the template is:

<?php echo do_shortcode("[metaslider id=27]"); ?>

And to display the output of custom fields I have:

<?php echo get_post_meta($post->ID, 'slider', true); ?>

Each of these works on its own, but I need to combine them, so that the client doesn't have to edit the template just to add the shortcode. So I am thinking something like this should work:

<?php echo do_shortcode("[<?php echo get_post_meta($post->ID, 'slider', true); ?>]"); ?>

... but it doesn't.

Many thanks in advance for any help with this.

C


Solution

  • Simply pass the return value of get_post_meta (which would contain the shortcode as far as I understood) to the do_shortcode function as an argument:

    do_shortcode(get_post_meta($post->ID, 'slider', true));