从WordPress帖子中获取有限的纯文本摘录?
问题描述:
我在自己的主题模板中使用"The Loop"来获取WordPress的最后三篇文章.
I'm using "The Loop" in my own theme template to get the last three posts from WordPress.
<?php
$args = array( 'numberposts' => 3 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<!-- DATE -->
<div class="date">
<?php the_time('m F Y');?>
</div>
<!-- TITLE -->
<div class="title">
<?php the_title(); ?>
</div>
<!-- SNIPPET -->
<div class="content">
<?php the_excerpt(); ?>
</div>
<?php endforeach; ?>
一切正常-除了the_excerpt()
.我需要将帖子中的15-20个纯文本单词显示为预览,而不是完整摘录或整个帖子内容正文.我该怎么做呢?
Everything is working fine - except the_excerpt()
. I need about 15-20 words of plain text from the post to show as a preview, instead of the full excerpt or the entire post content body. How do I go about doing this?
答
如果没有摘录,您可以尝试使用类似的方法抓住帖子的前20个字.
You could try using something like this to grab the first 20 words of the post if there is no excerpt available.
$content = get_the_content();
echo substr($content, 0, 20);