【问题标题】:Upload post to WP makes <br> tags disappear将帖子上传到 WP 会使 <br> 标签消失
【发布时间】:2021-09-08 16:53:35
【问题描述】:

我使用https://github.com/maxcutler/python-wordpress-xmlrpc 将文章上传到Wordpress。我注意到&lt;div&gt; 内的文本行自动被&lt;p&gt; 标签包裹,&lt;br&gt; 标签被删除。

有没有办法保留&lt;br&gt; 标签?

post = WordPressPost()
post.title = 'title'
post.content = '<div>This is a test article<br><br>There are some linebreaks<br><br>here<br></div>'
post.post_status = 'publish'
wp.call(NewPost(post))

创建此帖子:

<div>This is a test article
<p>&nbsp;</p>
<p>There are some linebreaks</p>
<p>here</p>
</div>

【问题讨论】:

标签: python wordpress


【解决方案1】:

您可以将您感兴趣的标签列入白名单。Wordpress 有一个特殊功能:

wp_ksesDocs

例如,您可以将&lt;br&gt; 标签列入白名单,如下所示:

add_filter('the_content', 'your_theme_whitelist_tags');

function your_theme_whitelist_tags($content)
{

  $whitelist_tags = array(

    'br' => array()

  );

  $content = wp_kses($content, $whitelist_tags);

  return $content;
}

保留&lt;br&gt; 标签并删除其他html标签。如果您还想保留 &lt;div&gt; 标签,也可以将该标签添加到白名单数组中。

代码进入您的活动主题的functions.php 文件。

让我知道你是否可以让它工作!

【讨论】:

    猜你喜欢
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多