简体   繁体   中英

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

只需将get_post_meta的返回值(包含我所理解的do_shortcode代码)作为参数传递给do_shortcode函数:

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

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