如何动态限制wordpress the_excerpt

问题描述

我正在尝试限制wordpress文章摘录,我尝试了一些方法来做,但是所有这些都不是我需要的东西,简而言之,我想用我在各处使用的数字来限制Postgresql摘录。有所不同。

例如,我需要做一些类似的事情: <?PHP the_excerpt('30') ?> 使用这部分代码,我想将摘录限制为30个字符,而在另一个地方,我想使用其他值,例如: <?PHP the_excerpt('150') ?> 是在wordpress中吗?

解决方法

您可以使用以下内容缩短摘录:

function shorten( $s,$num,$dots ) {
        if ( $num < mb_strlen( $s ) ) {
            $fs = mb_substr( $s,$num );

            for ( $i = mb_strlen( $fs ); $i >= 0; $i -- ) {
                if ( mb_substr( $fs,$i,1 ) == ' ' ) {
                    return mb_substr( $fs,$i + 1 ) . $dots;
                }
            }

            return $fs . $dots;

        } else {
            return $s;
        }
    }

然后您可以这样称呼它:shorten(get_the_excerpt(),40,'...')(如果需要,可以用其他任何东西代替圆点)。

Source