按ID获取the_content并保留WordPress中的

-tag

问题描述:

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(']]>', ']]&gt;', $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?

我想通过ID获取自定义帖子类型的内容,并以短代码形式返回。 p >

我找到了多种方法。 但是所有这些都从内容中删除了&lt; p&gt; code> -tags。 p>

这是我尝试过的: p>

  //从元字段中获取ID 
 $ post_id = $ id; 
 
 
 //方法1 
 $ banner_content = get_post($ post_id); 
 $ content = $  banner_content-&gt; post_content; 
 
 
 //方法2 
 $ content = apply_filters('the_content',get_post_field('post_content',$ post_id)); 
 
 
 //方法3 
  $ content_post = get_post($ post_id); 
 $ content = $ content_post-&gt; post_content; 
 $ content = apply_filters('the_content',$ content); 
 $ content = str_replace(']]&gt;',  ']]&amp; gt;',$ content); 
 
 
 //方法4 
 $ content = get_post_field('post_content',$ post_id); 
 
 
 //在短代码中输出 
return'&lt; div&gt;'。$ content。'&lt; / div&gt;'; 
  code>  pre> 
 
 

有没有办法保留&lt; p&gt; ; code> -tag? p> div>

I found the answer here: Wordpress - the_content doens't give p tags back

For my example, this works:

return '<div>'.wpautop($content).'</div>';