限制字符串的文本量


Limit amount of text of a string

我正在使用WordPress,我有一个帖子,我想抓住并在我网站的首页上发布信息帖子。

我已经设法让它工作,但它发布了整篇文章,我想只发布一个大概 30 个字符,最后有一个"阅读更多"按钮。

下面是我正在使用的代码。由于我对 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, '...');?>