user3396122 user3396122 - 1 year ago 122
PHP Question

Wordpress: shortcode doesn't work when getting post content using function get_post_field()

I want to get the post content by id outside the loop, so i am using following code:

echo get_post_field('post_content', $postid);

It works fine, however, if the post contains any shortcodes, the shortcodes don't work properly. It only echoes the shortcode as plain text.

Example: I'm using following code in editor to display image and caption text inder the image:

[caption id="attachment_23" align="alignnone" width="300"]<img class="size-medium wp-image-23 " alt="" src="http://localhost/wordpress/wp-content/uploads/2014/03/Desert-300x225.jpg" width="300" height="225" /> this is caption[/caption]

But when i get this post content using function
, Instead of displaying caption text, it displays:

[caption id="attachment_23" align="alignnone" width="300"]this is caption[/caption]

Any solution?

N.B: I am using ajax to get the contents

Answer Source

This will work:

echo do_shortcode(get_post_field('post_content', $postid));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download