dtu1747 2019-05-09 16:55
浏览 118
已采纳

按ID获取the_content并保留WordPress中的<p> -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?

  • 写回答

1条回答 默认 最新

  • dqwh1202 2019-05-09 17:05
    关注

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

    For my example, this works:

    return '<div>'.wpautop($content).'</div>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据