我正在创建一个Wordpress主题,我有一个区域显示标题和摘录,但摘录需要不同的长度取决于标题的长度(即标题和摘录都占据固定空间) 。因此,我需要一种动态改变摘录长度的方法,具体取决于帖子标题的长度。
我见过两段可能有用的代码片段。第一个是在下面给出的:
<?php
// Variable & intelligent excerpt length.
function print_excerpt($length) { // Max excerpt length. Length is set in characters
global $post;
$text = $post->post_excerpt;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
}
$text = strip_shortcodes($text); // optional, recommended
$text = strip_tags($text); // use ' $text = strip_tags($text,'<p><a>'); ' if you want to keep some tags
$text = substr($text,0,$length);
$excerpt = reverse_strrchr($text, '.', 1);
if( $excerpt ) {
echo apply_filters('the_excerpt',$excerpt);
} else {
echo apply_filters('the_excerpt',$text);
}
}
// Returns the portion of haystack which goes until the last occurrence of needle
function reverse_strrchr($haystack, $needle, $trail) {
return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
}
然后可以简单地使用,例如<?php print_excerpt(50); ?>
。下面的代码根据标题长度更改摘录长度。我将如何组合这两段代码?
<?php
// Dynamically resize excerpt according to title length
$rem_len = ""; //clear variable
$title_len = strlen($post->post_title); //get length of title
if($title_len <= 35){
$rem_len=188; //calc space remaining for excerpt
}elseif($title_len <= 70){
$rem_len=146;
}elseif($title_len <= 105){
$rem_len=104;
}elseif($title_len <= 140){
$rem_len=62;
}
$trunc_ex = substr($post->post_excerpt, 0, $rem_len); //truncate excerpt to fit remaining space
if(strlen($trunc_ex) < strlen($post->post_excerpt)) $trunc_ex = $trunc_ex . " [...]";
echo "<p>" . $trunc_ex . "</p>"; //display excerpt
?>
答案 0 :(得分:0)
解决:
<?php
// Variable & intelligent excerpt length.
function print_excerpt($title) { // Max excerpt length. Length is set in characters
global $post;
$rem_len = ""; //clear variable
$title_len = strlen($title); //get length of title
$excerpt_line=40;
if($title_len <= 17){
$rem_len=$excerpt_line*8; //calc space remaining for excerpt
}elseif($title_len <= 34){
$rem_len=$excerpt_line*7;
}elseif($title_len <= 51){
$rem_len=$excerpt_line*6;
}elseif($title_len <= 68){
$rem_len=$excerpt_line*5;
}elseif($title_len <= 85){
$rem_len=$excerpt_line*4;
}
$text = $post->post_excerpt;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
}
$text = strip_shortcodes($text); // optional, recommended
$text = strip_tags($text,'<p>'); // use ' $text = strip_tags($text,'<p><a>'); ' if you want to keep some tags
$text = substr($text,0,$rem_len);
$excerpt = reverse_strrchr($text, ' ', 1);
if( $excerpt ) {
echo apply_filters('the_excerpt',$excerpt);
} else {
echo apply_filters('the_excerpt',$text);
}
}
// Returns the portion of haystack which goes until the last occurrence of needle
function reverse_strrchr($haystack, $needle, $trail) {
return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
}
?>