I’ve created a simple text custom meta field for pages. I can successfully enter plain text which prints on the frontend of the site with no problem.
I want to add shortcodes also. If I add a shortcode to the field, the shortcode prints to the front end as plain text.
The code I’m using:
<?php $meta = get_post_meta($post->ID, 'intSlider', true); ?>
<div id="sliderWrap">
<div id="slider" class="floatLeft">
<? echo $meta; ?>
</div>
</div>
I’ve looked into using the below code but not having much luck:
<?php echo ( do_shortcode( get_post_meta( $post->ID , 'intSlider' , true ) ) ); ?>
Any help much appreciated
Thanks
2 Answers
You can do this by using ‘the_content’ filter. That way, WordPress will treat the content as it was came from the editor field and execute all the shortcodes:
<?php $meta = get_post_meta($post->ID, 'intSlider', true); ?>
<div id="sliderWrap">
<div id="slider" class="floatLeft">
<? echo apply_filters('the_content', $meta); ?>
</div>
</div>
Just be careful because it will wrap the content arround P tags. To solve that, you can do a simple replace to remove it:
...
<?php
$content = apply_filters('the_content', $meta);
$content = str_replace(array('<p>', '</p>'), '', $content);
?>
...
Hope it helps 🙂