通过ID获取the_content并在WordPress中保留<p> -tag

克雷

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

我发现有多种方法可以做到这一点。但是所有这些<p>都从内容中删除了-tags。

这是我尝试过的:

// 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>';

有没有办法保留<p>-tag?

克雷

我在这里找到了答案:WordPress-the_content不给p标签

以我的示例为例:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章