POST_CONTENT中更多标签后的字数限制

时间:2015-02-25 作者:Skotlive

我使用以下代码隐藏摘要并仅在循环中添加更多标记后显示内容:

<?php
$after_more = explode(
    \'<!--more-->\', 
    $post->post_content
); 
if( $after_more[1] ) { 
    echo $after_more[1]; 
} else {
    echo $after_more[0]; 
}
?>
是否只显示前50个单词而不是整个帖子内容?我想隐藏挑逗并在标签后显示50个单词。

2 个回复
最合适的回答,由SO网友:Robert hue 整理而成

使用wp_trim_words 函数将内容限制为一定数量的单词,并返回修剪后的文本。示例使用wp_trim_words 作用

<?php

    $content = get_the_content();
    $trimmed_content = wp_trim_words( $content, 50, NULL );
    echo $trimmed_content;

?>
所以我补充道wp_trim_words 函数在代码中获取50个单词<!-- more -->.

<?php
    $after_more = explode( \'<!--more-->\', $post->post_content );

    if( $after_more[1] ) {
        $content = $after_more[1];
    } else {
        $content = get_the_content();
    }

    $trimmed_content = wp_trim_words( $content, 50, NULL );
    echo $trimmed_content;
?>
编辑以显示内容中的50个单词(如果没有<!--more--> 在帖子内容中。

SO网友:birgire

A) The<!--more--> 评论:

这里有一句话:

echo wp_trim_words( strip_shortcodes( strip_tags( get_the_content( \'\', true ) ) ), 50 );
我们使用的第二个参数get_the_content() 将挑逗者隐藏在<!--more--> 参与帖子内容。

B) The<!--noteaser--> 注释:

注意,存在<!--noteaser--> 注释,我们可以使用它来控制帖子内容的摘要显示:

....
<!--more--><!--noteaser-->
...
在这种情况下,我们将使用:

echo wp_trim_words( strip_shortcodes( strip_tags( get_the_content( \'\', false ) ) ), 50 );
如果需要,我们还可以对上述输出应用其他过滤器。

在这种情况下,我们还可以使用:

echo wp_trim_excerpt();
然后用excerpt_length, excerpt_more, the_content, 和wp_trim_excerpt 过滤器。

《星际迷航》Lorem Ipsum的一个例子:对于上述案例A:

Before:

Exceeding reaction chamber thermal limit. 
We have begun power-supply calibration. 
<!--more-->
Force fields have been established on all turbo lifts and crawlways. 
Computer, run a level-two diagnostic on warp-drive systems. 
Antimatter containment positive. 
Warp drive within normal parameters. 
I read an ion trail characteristic of a freighter escape pod. 
The bomb had a molecular-decay detonator. 
Detecting some unusual fluctuations in subspace frequencies.
Sensors indicate no shuttle or other ships in this sector. 
According to coordinates, we have travelled 7,000 light years 
and are located near the system J-25. 
Tractor beam released, sir. 
Force field maintaining our hull integrity. 

After:

Force fields have been established on all turbo lifts and crawlways. 
Computer, run a level-two diagnostic on warp-drive systems. 
Antimatter containment positive. 
Warp drive within normal parameters. 
I read an ion trail characteristic of a freighter escape pod. 
The bomb had a molecular-decay detonator. 
Detecting some unusual fluctuations in subspace...

结束

相关推荐