WordPress功能/模板标签获取内容的前n个单词
-
-
您正在寻找摘录.我对此做了完整的回答.看看[这里](http://wordpress.stackexchange.com/a/141136/31545)You are looking for the excerpt. I done a complete answer on that. Have a look [here](http://wordpress.stackexchange.com/a/141136/31545)
- 1
- 2014-04-16
- Pieter Goosen
-
1 个回答
- 投票数
-
- 2014-04-16
是的,
wp_trim_words()
:<?php $trimmed = wp_trim_words( $text, $num_words = 55, $more = null ); ?>
或者您的情况:
<?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 55 ) ); ?>
Yep,
wp_trim_words()
:<?php $trimmed = wp_trim_words( $text, $num_words = 55, $more = null ); ?>
Or in your case:
<?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 55 ) ); ?>
-
@JaromírtheGreenhorn如果您想在`get_the_content()`上使用它,则需要事先剥离HTML(请参阅修订).@JaromírtheGreenhorn If you'd like to use this on `get_the_content()`, you'll need to strip HTML beforehand (see revision).
- 0
- 2014-04-16
- TheDeadMedic
-
这似乎对我有用,无需额外剥离,但很高兴知道.It seems to work for me without extra strip down, but good to know.
- 0
- 2014-04-16
- Jaromír the Greenhorn
-
我仍然强烈建议您使用它-它可能现在可以使用,但是最终您将得到格式错误的HTML.I would still strongly advise that you use it - it might be working now, but eventually you'll end up with malformed HTML.
- 2
- 2014-04-17
- TheDeadMedic
-
(*ironic *)好吧,师父,我保证会听话!(*ironic*) Alright, Master, I promise to be obedient!
- 0
- 2014-04-17
- Jaromír the Greenhorn
好的,这可能是重复的,我只是无法将我的想法输入适当的搜索字词,对此表示歉意!
是否有内置函数或模板标签来获取内容的前
n
个单词?我的意思是the_content()
.谢谢!