php 从 WordPress 帖子中获取有限的纯文本摘录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5307410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:57:05  来源:igfitidea点击:

Get a limited, text-only excerpt from a WordPress post?

phparrayswordpressfunctionforeach

提问by n a

I'm using "The Loop" in my own theme template to get the last three posts from WordPress.

我在自己的主题模板中使用“The Loop”从 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; ?>

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?

一切正常 - 除了the_excerpt(). 我需要帖子中大约 15-20 个单词的纯文本作为预览显示,而不是完整的摘录或整个帖子内容正文。我该怎么做?

采纳答案by Tianbo84

You could try using something like this to grab the first 20 words of the post if there is no excerpt available.

如果没有可用的摘录,您可以尝试使用类似的方法来获取帖子的前 20 个单词。

$content = get_the_content();
echo substr($content, 0, 20);

回答by Samuel Liew

Avoid using substr(). Why?

避免使用substr(). 为什么?

substr()truncates based on character count, NOT whole words, and the last word would most likely be truncated. It could also truncate the end HTML tag(s) and return malformed HTML, screwing up the rest of your layout.

substr()根据字符数进行截断,而不是整个单词,最后一个单词很可能会被截断。它还可能截断 HTML 结束标记并返回格式错误的 HTML,从而破坏布局的其余部分。

Don't re-invent the wheel!

不要重新发明轮子!

WordPress 3.3 onwards has a new core function called wp_trim_words()

WordPress 3.3 以后有一个新的核心功能叫做 wp_trim_words()

Parameter breakdown:

参数分解:

wp_trim_words($text, $num_words, $more);


$text
    (string) (required) Text to trim
    Default: None

$num_words
    (integer) (optional) Number of words
    Default: 55

$more
    (string) (optional) What to append if $text needs to be trimmed.
    Default: '…'

Example usages:

示例用法:

<?php echo wp_trim_words(get_the_content(), 40, '...'); ?>

<?php echo wp_trim_words(get_the_excerpt(), 20, '... read more &gt;'); ?>

回答by Ram Ch. Bachkheti

try this :

尝试这个 :

Post contains images :

帖子包含图片:

$content = get_the_content();
$content = apply_filters('the_content', $content);
$content = str_replace(']]>',']]&gt;', $content);
echo substr(strip_tags($content),0,100); 

and without images:

并且没有图像:

 $content = get_the_content();
 echo substr($content, 0, 25);

回答by Manish Negi

Put this code in functions.php

将此代码放在functions.php中

function new_excerpt_length($length) {
  return 20;}
add_filter('excerpt_length', 'new_excerpt_length');

And just call this function from your template page or index.php file

只需从您的模板页面或 index.php 文件中调用此函数

the_excerpt();