You can add it straight to the template before the header in most cases...
<?php /* Template Name: Products */ ?>
<?php
add_filter( \'excerpt_length\', function( $length ) { return 10; }, 999);
get_header();
?>
如果你更愿意加入
functions.php
:
Method 2: In the loop
$slug = get_page_template_slug($post->ID);
if(\'page-products.php\' == $slug) {
add_filter( \'excerpt_length\', function( $length ) {
return 20;
}, 999);
}
一
if ( is_page_template( \'page-products.php\' ) ) {
add_filter( \'excerpt_length\', function( $length ) {
return 20;
}, 999);
}
This could also be used to get the template....
global $post;
$template = get_post_meta($post->ID,\'_wp_page_template\',true);
Or possibly even this (untested):
add_action(\'get_header\', function() {
if ( is_page( array( 37, \'Products\' ) ) ) {
add_filter( \'excerpt_length\', function( $length ) {
return 20;
}, 999);
}
}, 1);
Or as seen here, you can create your own function to allow the excerpt length to be determined on a need to need basis by the amount of characters.
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;
}
要显示的内容:<?php echo excerpt(50); ?>