限制字符串的文本数量

Limit amount of text of a string

我正在使用 WordPress,我有一个 post,我想获取它并将信息 post 放在我网站的首页。

我已经设法让它工作了,但它 post 是整篇文章,我只想 post 大概 30 个字符,在 "read more" 按钮结尾。

下面是我正在使用的代码。由于我对 Php 比较陌生,所以有点卡住了。

    <div class="entry-content">
        <?php if ( siteorigin_setting( 'blog_archive_content' ) == 'excerpt' ) the_excerpt(); else the_content(); ?>

        <?php
            wp_link_pages( array(
                'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'ultra' ) . '</span>',
                'after'  => '</div>',
                'link_before' => '<span>',
                'link_after'  => '</span>',
            ) );
        ?>
    </div><!-- .entry-content -->

php 文件的完整内容如下:

<?php
/**
 * @package ultra
 * @since ultra 0.9
 * @license GPL 2.0
 */
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <?php if ( ! is_single() && has_post_thumbnail() && siteorigin_setting( 'blog_archive_featured_image' ) ) : ?>

        <div class="entry-thumbnail">
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                <?php the_post_thumbnail(); ?>
            </a>
        </div>

    <?php elseif ( is_single() && has_post_thumbnail() && siteorigin_setting( 'blog_archive_featured_image' ) ) : ?>

        <div class="entry-thumbnail">
            <?php the_post_thumbnail(); ?>

        </div>

    <?php endif; ?>
    <header class="entry-header">
        <?php the_title( sprintf( '<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>

        <?php if ( 'post' == get_post_type() ) : ?>


        <?php endif; ?>
    </header><!-- .entry-header -->

    <div class="entry-content">
        <?php if ( siteorigin_setting( 'blog_archive_content' ) == 'excerpt' ) the_excerpt(); else the_content(); ?>

        <?php
            wp_link_pages( array(
                'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'ultra' ) . '</span>',
                'after'  => '</div>',
                'link_before' => '<span>',
                'link_after'  => '</span>',
            ) );
        ?>
    </div><!-- .entry-content -->



</article><!-- #post-## -->

您可以使用 wp_trim_words,它会为您提供一定数量的字词(而非字符)。

<?php  echo wp_trim_words( get_the_content(), 5, '...<a href="'. get_permalink().'">Read More</a>' );?>

或使用 mb_strimwidth 这会给你一个字符限制。

<?php echo mb_strimwidth(get_the_content(), 0, 30, '...');?>