从WordPress帖子中获取有限的纯文本摘录? [英] Get a limited, text-only excerpt from a WordPress post?

查看:95
本文介绍了从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);

这篇关于从WordPress帖子中获取有限的纯文本摘录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆