Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a Custom Post Type that has several custom fields. One of the fields is multi-line and includes text with p tags.

When I view this field in the visual editor the p tags are all there.

I suspect the issue is how I am displaying the data in a custom template.

This is the code that I am using in the template to pull this particular custom field

 <?php $key="story"; echo get_post_meta($post->ID, $key, true); ?>

Is it the get_post_meta that is stripping these tags or something else? If it is this what should I be using instead?

Thanks for any clues

share|improve this question
add comment

2 Answers

Add this to your functions.php file:

add_filter( 'meta_content', 'wptexturize' );
add_filter( 'meta_content', 'convert_smilies' );
add_filter( 'meta_content', 'convert_chars' );
add_filter( 'meta_content', 'wpautop' );
add_filter( 'meta_content', 'shortcode_unautop' );
add_filter( 'meta_content', 'prepend_attachment' );

Then use this code to pull in the result:

<?php $story_content = get_post_meta($post->ID, 'story', true); echo apply_filters('meta_content', $story_content); ?>
share|improve this answer
    
Thank you very much I tried this and it works fine. Is there any benefit in using this over Lukas J code above? Or maybe the question should be what is the advantage of this approach. Sorry a bit of a newbie at Wordpress coding. –  Colin Oct 24 '13 at 16:21
    
The difference is preference. The solution I have uses the main WP function for displaying content and creates a reusable filter for other areas on your site if you use apply_filters. As I normally use this solution when I'm creating custom wysiwyg editors, there may be some filters added that you don't need in functions.php. For your situation, you probably only need wpautop. And if you don't need the filter to be reusable, you can probably just use `<?php $story_content = get_post_meta($post->ID, 'story', true); echo wpautop( $story_content ); ?> and remove the code from functions.php. –  Seth Alling Oct 24 '13 at 18:38
add comment

Try this:

$key="story";
$content = get_post_meta($post->ID, $key, true);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
echo $content;

This should get your custom field and apply filters (with p tags)

share|improve this answer
    
Thank you very much I tried this and it works fine. Is there any benefit in using this over Seth's code below? Or maybe the question should be what is the advantage of this approach. Sorry a bit of a newbie at Wordpress coding. –  Colin Oct 24 '13 at 16:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.