Have a custom page template and trying to make the excerpt length dynamical depending on what page you are in.
Functions.php
<?php
function custom_excerpt_length($length){
if (is_page(15)) {
return 20;
} else {
return 150;
}
}
add_filter ( 'excerpt_length', 'custom_excerpt_length');
?>
cant get is_page to work from functions in my custom template page.
If i try echo is_page(15);
in the static custom template value is true.
i'am little confused. Is it possible to call is_page()
from functions.php (WP 3.1.2)
var_dump( is_page( 15 ) )
in yourcustom_excerpt_length()
function? – Jan Fabry May 11 '11 at 8:44var_dump(is_page(15));
from functions.php its always bool(false) but if try it from header.php the value is true. Sorry about my English – ide79 May 11 '11 at 10:04functions.php
(not in a function in that file) it will be executed too early, before WordPress knows it is a page or not. If you do it in thecustom_excerpt_length()
function it should work, because this function is called byexcerpt_length
, and at that moment WordPress should know that it is on a page. - But you say you do this in a special page template - a separate file in your template directory? Then you can also specify it there. (Use@Jan
when you reply and I get a notification.) – Jan Fabry May 11 '11 at 10:12var_dump(is_page(15));
inside the function i get bool(false). The function works but theis_page(15);
don't work inside the function. I think it is best to put the function without theis_page();
in the custom template page (it is a separate file in template directory). So the excerpt does not interfere the other pages. – ide79 May 11 '11 at 11:34wp_reset_query();
before your conditionalis_page(15)
. – Michael May 11 '11 at 17:01