我最近读到this awesome post 我需要帮助,弄清楚如何只在有更多内容要显示的帖子上显示“阅读更多”链接。目前,它显示在每个帖子上,即使是一行很短的帖子。如果您有任何帮助,我将不胜感激,我基本上只是复制/粘贴了Pieter的代码,并添加了一个小的内容:
// Begin Excerpt Code
function wpse_allowedtags() {
// Add custom tags to this string
return \'<script>,<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>\';
}
if ( ! function_exists( \'wpse_custom_wp_trim_excerpt\' ) ) :
function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
$raw_excerpt = $wpse_excerpt;
if ( \'\' == $wpse_excerpt ) {
$wpse_excerpt = get_the_content(\'\');
$wpse_excerpt = strip_shortcodes( $wpse_excerpt );
$wpse_excerpt = apply_filters(\'the_content\', $wpse_excerpt);
$wpse_excerpt = str_replace(\']]>\', \']]>\', $wpse_excerpt);
$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /*IF you need to allow just certain tags. Delete if all tags are allowed */
//Set the excerpt word count and only break after sentence is complete.
$excerpt_word_count = 75;
$excerpt_length = apply_filters(\'excerpt_length\', $excerpt_word_count);
$tokens = array();
$excerptOutput = \'\';
$count = 0;
// Divide the string into tokens; HTML tags, or words, followed by any whitespace
preg_match_all(\'/(<[^>]+>|[^<>\\s]+)\\s*/u\', $wpse_excerpt, $tokens);
foreach ($tokens[0] as $token) {
if ($count >= $excerpt_length && preg_match(\'/[\\,\\;\\?\\.\\!]\\s*$/uS\', $token)) {
// Limit reached, continue until , ; ? . or ! occur at the end
$excerptOutput .= trim($token);
break;
}
// Add words to complete sentence
$count++;
// Append what\'s left of the token
$excerptOutput .= $token;
}
$wpse_excerpt = trim(force_balance_tags($excerptOutput));
$excerpt_end = \' <a href="\'. esc_url( get_permalink() ) . \'">\' . \' » \' . sprintf(__( \'Read more about: %s »\', \'wpse\' ), get_the_title()) . \'</a>\';
$excerpt_more = apply_filters(\'excerpt_more\', \' \' . $excerpt_end);
//$pos = strrpos($wpse_excerpt, \'</\');
//if ($pos !== false)
// Inside last HTML tag
//$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
//else
// After the content
$wpse_excerpt .= $excerpt_more; /*Add read more in new paragraph */
return $wpse_excerpt;
}
return apply_filters(\'wpse_custom_wp_trim_excerpt\', $wpse_excerpt, $raw_excerpt);
}
endif;
remove_filter(\'get_the_excerpt\', \'wp_trim_excerpt\');
add_filter(\'get_the_excerpt\', \'wpse_custom_wp_trim_excerpt\');
// Replaces the excerpt "more" text by a link
function new_excerpt_more($more) {
global $post;
return \'<br /><br /><a class="moretag btn-lg" href="\'. get_permalink($post->ID) . \'"> Read more</a>\';
}
add_filter(\'excerpt_more\', \'new_excerpt_more\');
// End Excerpt Code
最合适的回答,由SO网友:Pieter Goosen 整理而成
事实上,我从来没有在你提到的我的答案中添加这一部分。要从摘录中删除“阅读更多”链接非常简单,只需比较$count
具有$excerpt_length
. $count
将始终介于0
以及指定给的值$excerpt_length
. 因此,我们要做的是在一个条件中添加read more链接,条件是如果$count
小于$excerpt_length
, 我们不应该显示“阅读更多”
要将这些全部放在代码中,只需替换以下部分
$excerpt_end = \' <a href="\'. esc_url( get_permalink() ) . \'">\' . \' » \' . sprintf(__( \'Read more about: %s »\', \'wpse\' ), get_the_title()) . \'</a>\';
$excerpt_more = apply_filters(\'excerpt_more\', \' \' . $excerpt_end);
//$pos = strrpos($wpse_excerpt, \'</\');
//if ($pos !== false)
// Inside last HTML tag
//$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
//else
// After the content
$wpse_excerpt .= $excerpt_more; /*Add read more in new paragraph */
使用
if ( $count >= $excerpt_word_count ) {
$excerpt_end = \' <a href="\'. esc_url( get_permalink() ) . \'">\' . \' » \' . sprintf(__( \'Read more about: %s »\', \'wpse\' ), get_the_title()) . \'</a>\';
$excerpt_more = apply_filters(\'excerpt_more\', \' \' . $excerpt_end);
//$pos = strrpos($wpse_excerpt, \'</\');
//if ($pos !== false)
// Inside last HTML tag
//$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
//else
// After the content
$wpse_excerpt .= $excerpt_more; /*Add read more in new paragraph */
}