I want to get the content of a custom post type by an ID an return it in a shortcode.
I found multiple ways to do that. But all of them remove the <p>
-tags from the content.
Here is what I've tried:
// Get the ID from a meta field
$post_id = $id;
// Method 1
$banner_content = get_post($post_id);
$content = $banner_content->post_content;
// Method 2
$content = apply_filters('the_content', get_post_field('post_content', $post_id));
// Method 3
$content_post = get_post($post_id);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
// Method 4
$content = get_post_field('post_content', $post_id);
// Output in a shortcode
return '<div>'.$content.'</div>';
Is there a way to preserve the <p>
-tag?