問題描述
正如在標題中所説,我正在 WordPress 中尋找多個摘錄長度。
我明白你可以在 functions.php 中做到這一點:
function twentyten_excerpt_length( $length ) {
return 15;
}
add_filter( 'excerpt_length', 'twentyten_excerpt_length' );
我想知道的是,您可以如何將這些數據中的多個返回不同的數值,以便我可以獲得邊欄循環的較短摘要,特色循環的更長的摘錄以及主要文章的最長摘錄。
在模板中使用這些東西:
<?php the_excerpt('length-short') ?>
<?php the_excerpt('length-medium') ?>
<?php the_excerpt('length-long') ?>
乾杯,戴夫
最佳解決方案
怎麼樣…
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`[[^]]*]`','',$excerpt);
return $excerpt;
}
function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/[.+]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
那麼在你的模板代碼中你只是使用..
<?php echo excerpt(25); ?>
來自:http://bavotasan.com/tutorials/limiting-the-number-of-words-in-your-excerpt-or-content-in-wordpress/
次佳解決方案
至於現在,你可以升級 Marty 的回覆:
function excerpt($limit) {
return wp_trim_words(get_the_excerpt(), $limit);
}
您也可以通過以下方式定義自定義’read more’ 鏈接:
function custom_read_more() {
return '... <a class="read-more" href="'.get_permalink(get_the_ID()).'">more »</a>';
}
function excerpt($limit) {
return wp_trim_words(get_the_excerpt(), $limit, custom_read_more());
}
第三種解決方案
這是我想出來的。
將其添加到您的 functions.php
class Excerpt {
// Default length (by WordPress)
public static $length = 55;
// So you can call: my_excerpt('short');
public static $types = array(
'short' => 25,
'regular' => 55,
'long' => 100
);
/**
* Sets the length for the excerpt,
* then it adds the WP filter
* And automatically calls the_excerpt();
*
* @param string $new_length
* @return void
* @author Baylor Rae'
*/
public static function length($new_length = 55) {
Excerpt::$length = $new_length;
add_filter('excerpt_length', 'Excerpt::new_length');
Excerpt::output();
}
// Tells WP the new length
public static function new_length() {
if( isset(Excerpt::$types[Excerpt::$length]) )
return Excerpt::$types[Excerpt::$length];
else
return Excerpt::$length;
}
// Echoes out the excerpt
public static function output() {
the_excerpt();
}
}
// An alias to the class
function my_excerpt($length = 55) {
Excerpt::length($length);
}
它可以像這樣使用。
my_excerpt('short'); // calls the defined short excerpt length
my_excerpt(40); // 40 chars
這是我知道添加過濾器的最簡單的方法,可以從一個函數調用。
第四種方案
我也在尋找這個功能,這裏的大多數功能是好的和靈活的。對於我自己的案例,我正在尋找一個解決方案,僅在特定頁面上顯示不同的摘錄長度。我使用這個:
function custom_excerpt_length( $length ) {
return (is_front_page()) ? 15 : 25;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
將此代碼粘貼到主題 functions.php 文件中。
第五種方案
回到馬蒂的回覆:
我知道這個答覆已經出版了一年多,但是比以前更好。為了使 WordPress 默認值為 55 的限制,您需要替換此行:
$excerpt = explode(' ', get_the_excerpt(), $limit);
用這一行:
$excerpt = explode(' ', get_the_content(), $limit);
否則,該功能僅適用於已經存在的 trimmed-down 文本。
第六種方案
我想我們現在可以用 wp_trim_words see here 。不知道使用這個功能需要額外的數據轉義和消毒,但它看起來很有趣。
第七種方案
你可以添加到你的 functions.php 文件這個函數
function custom_length_excerpt($word_count_limit) {
$content = wp_strip_all_tags(get_the_content() , true );
echo wp_trim_words($content, $word_count_limit);
}
然後在這樣的模板中調用它
<p><?php custom_length_excerpt(50); ?>
wp_strip_all_tags 應該防止雜散 html 標籤破壞頁面。
功能文件
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。