我有这个功能。
function trim_the_content( $the_contents = '', $read_more_tag = '...READ MORE', $perma_link_to = '', $all_words = 100 ) {
// make the list of allowed tags
$allowed_tags = array( 'a', 'abbr', 'b', 'blockquote', 'b', 'cite', 'code', 'div', 'em', 'fon', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'label', 'i', 'p', 'pre', 'span', 'strong', 'title', 'ul', 'ol', 'li', 'object', 'embed', 'param' );
if( $the_contents != '' && $all_words > 0 ) {
// process allowed tags
$allowed_tags = '<' . implode( '><', $allowed_tags ) . '>';
$the_contents = str_replace( ' ]]>', ' ]]>', $the_contents );
$the_contents = strip_tags( $the_contents, $allowed_tags );
// exclude HTML from counting words
if( $all_words > count( preg_split( '/[\s]+/', strip_tags( $the_contents ), -1 ) ) ) return $the_contents;
// count all
$all_chunks = preg_split( '/([\s]+)/', $the_contents, -1, PREG_SPLIT_DELIM_CAPTURE );
$the_contents = '';
$count_words = 0;
$enclosed_by_tag = false;
foreach( $all_chunks as $chunk ) {
// is tag opened?
if( 0 < preg_match( '/<[^>]*$/s', $chunk ) ) $enclosed_by_tag = true;
elseif( 0 < preg_match( '/>[^<]*$/s', $chunk ) ) $enclosed_by_tag = false;
if( !$enclosed_by_tag && '' != trim( $chunk ) && substr( $chunk, -1, 1 ) != '>' ) $count_words ++;
$the_contents .= $chunk;
if( $count_words >= $all_words && !$enclosed_by_tag ) break;
}
// note the class named 'more-link'. style it on your own
$the_contents = $the_contents . '' . $READ_MORE_TAG . '';
// native WordPress check for unclosed tags
$the_contents = force_balance_tags( $the_contents );
}
return $the_contents;
}
当我在索引中调用该函数时,缺少“Read More ...”。
<?php
// here's the point where we are about to display Post excerpt
$perma_link = get_permalink( $post->ID ); // current Post permalink
$content = get_the_content(); // save entire content in variable
$content = apply_filters( 'the_content', $content ); // WP bug fix
$content = str_replace( ']]>', ']]>', $content ); // ...as well
echo trim_the_content( $content, __( "READ MORE", "sofa_memento" ), $perma_link, 100 );
?>
有人看到我的问题吗?
答案 0 :(得分:3)
$the_contents = $the_contents . '' . $read_more_tag . '';