为函数中的字符和字数添加以下函数。php
function wordlimit($string, $limit)
{
$overflow = true;
$array = explode(" ", $string);
$output = \'\';
for ($i = 0; $i < $limit; $i++)
{
if (isset($array[$i]))
{
$output .= $array[$i] . " ";
}
else
{
$overflow = false;
}
}
return trim($output) . ($overflow === true ? "..." : \'\');
}
function charlimit($string,$limit){
$string = preg_replace(" (\\[.*?\\])",\'\',$string);
$string = strip_tags($string);
$string = trim(preg_replace( \'/\\s+/\', \' \', $string));
return strlen($string)<=$limit?$string:substr($string,0,$limit).\'...\';
}
然后在文件中使用
wordlimit(get_the_content( $post->ID ),160); 而不是
get_the_excerpt()<?php
/**
* Template part for displaying posts
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package arcvertex
*/
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="row">
<div class="col-md-3">
<div class="thumbnail alignleft" style="width:100%;">
<?php echo get_the_post_thumbnail(); ?>
</div>
</div>
<div class="col-md-9">
<header class="entry-header article">
<?php
if ( is_singular() ) :
the_title( \'<h1 class="entry-title">\', \'</h1>\' );
else :
the_title( \'<h2 class="entry-title"><a href="\' . esc_url( get_permalink() ) . \'" rel="bookmark">\', \'</a></h2>\' );
endif;
if ( \'post\' === get_post_type() ) : ?>
<div class="entry-meta">
<?php arcvertex_posted_on(); ?>
</div><!-- .entry-meta -->
<?php
endif; ?>
</header><!-- .entry-header -->
<div class="row">
<div class="col-md-12">
<div class="entry-content-article">
<?php echo wordlimit(get_the_content( $post->ID ),160);?>
</div><!-- .entry-content -->
<button type="button" class="btn btn-readmore">Read More...</button>
</div>
</div><!-- .Row -->
</div>
</div><!-- .Row -->
<hr class="half-rule-breadcrumb" style="margin-top:10px;" />
<footer class="entry-footer">
<?php arcvertex_entry_footer(); ?>
</footer><!-- .entry-footer -->
</article><!-- #post-<?php the_ID(); ?> -->