【问题标题】:I want to allow html tag when use the wp_trim_words我想在使用 wp_trim_words 时允许 html 标签
【发布时间】:2016-03-18 07:12:10
【问题描述】:

我想在锚标记中显示一些帖子内容并显示为链接,但我使用了wp_trim_words 函数我不知道如何允许 html 标记。

$more = ' <a href="'. get_permalink( get_the_ID() ) . '" class="readMoreBtn">Read More</a>';
$content = wp_trim_words( get_the_content(), 50, $more );
echo   do_shortcode($content);

【问题讨论】:

  • 但是通过 the_content 所有内容显示我想显示有限的内容。
  • 我完全错过了 wp_trim_words() 的标签剥离。更新了答案并对其进行了测试,现在@pia 应该可以工作了。

标签: wordpress trim


【解决方案1】:

感谢@dingo_d,但他在代码中错过了 wpautop()。这是更新后的代码。

$more = ' <a href="'. get_permalink( get_the_ID() ) . '" class="readMoreBtn">Read More</a>';

echo force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( wpautop(get_the_content()) ), 100, $more ) ) );

【讨论】:

  • 如果您要从中删除 wpautop(),它不会使用古腾堡编辑器生成额外的

    标签!但这很好用!

【解决方案2】:

啊啊啊我傻了。这将起作用(经过测试):

$more = ' <a href="'. get_permalink( get_the_ID() ) . '" class="readMoreBtn">Read More</a>';
 echo force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( get_the_content() ), 50, $more ) ) );

我花了一段时间才想起wp_trim_words() 从它所拥有的内容中去除了所有标签,所以你的 HTML 没有显示出来。所以我做了一点挖掘,发现了这个宝石:

Wordpress 轨道票#29533.

希望这会有所帮助。很抱歉之前的混乱:)

【讨论】:

  • 我在 [smart_track_player url="traffic.libsyn.com/abc/…" title="辛勤工作与 Max McGarity="abc" image="jakeszaraz.com/wp-content/uploads/2016/02/TheSeedREALFinal.jpg" 等内容中使用了播客播放器代码请点击这里给我一个诚实的评价和评论。但是当我使用你的代码时,html标签工作正常,但我的播客代码不能作为播放器工作。
  • 那是因为这段代码按原样输出html。我可以建议在您的内容中使用more tag&lt;-- more !--&gt;,并仅输出内容。 wp_trim_words() 会剥离html标签,不适合摘录。或者,我建议您查看 Pieter Goosen 的 excellent post 关于摘录。
  • 现在可以使用 $more = ' 阅读更多'来处理我的代码; $content= force_balance_tags(html_entity_decode(wp_trim_words(htmlentities(get_the_content()), 50, $more))); echo do_shortcode($content);
  • 太棒了!如果有帮助,您可以接受和/或投票赞成答案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2019-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多