我写了一些代码来定制excerpt_length
和excerpt_more
. 摘录长度适用于自定义帖子类型和普通帖子。但我不知道为什么excerpt_more
不适用于自定义帖子类型。下面是我的代码。
class Excerpt {
// Default length (by WordPress)
public static $length = 55;
// So you can call: doctoplus_excerpt(\'short\');
public static $types = array(
\'ten\'=>10,
\'few\' => 15,
\'short\' => 25,
\'regular\' => 55,
\'long\' => 100
);
//Default more
public static $more = \'...\';
//So you can call doctoplus_excerpt(\'short\',\'Read More\');
public static $moreTypes = array(
1 => \'Read More\',
2 => \'More\',
3 => \'View more\'
);
/**
* Sets the length for the excerpt,
* then it adds the WP filter
* And automatically calls the_excerpt();
*
* @param string $new_length
* @return void
* @author Girija
*/
public static function length($new_length = 55) {
Excerpt::$length = $new_length;
add_filter(\'excerpt_length\', \'Excerpt::new_length\', 999);
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;
}
public static function more($new_more = \'Read More\') {
Excerpt::$more = $new_more;
add_filter(\'excerpt_more\', \'Excerpt::new_more\', 999);
}
//Tell WP the custom more
public static function new_more() {
global $post;
if(isset(Excerpt::$moreTypes[Excerpt::$more]) ) {
return \' <a class="more-link" href="\'. get_permalink($post->ID) . \'">\'. __(Excerpt::$moreTypes[Excerpt::$more], \'doctoplus\') .\'</a>\';
} else {
return Excerpt::$more;
}
}
// Echoes out the excerpt
public static function output() {
the_excerpt();
}
}
// An alias to the class
function doctoplus_excerpt($length = 55, $more = "...") {
Excerpt::length($length);
Excerpt::more($more);
}
有人能帮我吗?我真的需要这样做。提前谢谢。