Have a custom page template and trying to make the excerpt length dynamical depending on what page you are in.



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)

  • 1
    Do you see anything if you call var_dump( is_page( 15 ) ) in your custom_excerpt_length() function?
    – Jan Fabry
    Commented May 11, 2011 at 8:44
  • that is the problem. If i var_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
    Commented May 11, 2011 at 10:04
  • If you just put it in functions.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 the custom_excerpt_length() function it should work, because this function is called by excerpt_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
    Commented May 11, 2011 at 10:12
  • @Jan when i try var_dump(is_page(15)); inside the function i get bool(false). The function works but the is_page(15); don't work inside the function. I think it is best to put the function without the is_page(); in the custom template page (it is a separate file in template directory). So the excerpt does not interfere the other pages.
    – ide79
    Commented May 11, 2011 at 11:34
  • 1
    always be aware that a custom query in the main code of the template can distort the original $query_string; make sure that all queries are restoring the original $query_string. or try to add wp_reset_query(); before your conditional is_page(15).
    – Michael
    Commented May 11, 2011 at 17:01

3 Answers 3


Using is_page() inside functions.php is fine, possible and working, and if you are echoing is_page(15) and getting true, that means you are on a page with the ID of 15.

the problem could be the priority parameter of the add_filter call, form the codex:

Make sure to set the priority correctly, else the default WordPress filter on this function will run last and override what you set here.

so change your code to :

add_filter ( 'excerpt_length', 'custom_excerpt_length',999);
  • I see no other filters on excerpt_length in the WordPress core, so I think this is an old recommendation. But it is always possible that in this case another piece of code (a plugin?) is hooking into this filter with a higher priority.
    – Jan Fabry
    Commented May 11, 2011 at 8:32
  • ok. so it should return true if i call echo is_page(15); from functions.php and entering to page id(15). When i try it from header.php it works but not from functions
    – ide79
    Commented May 11, 2011 at 9:34
  • it doesn't matter where the code is as long as its included and since its in a function in you functions.php its included. and it works just fine in my functions.php
    – Bainternet
    Commented May 11, 2011 at 9:44
  • Ok. Thanks for the information. Have to fix it somehow.
    – ide79
    Commented May 11, 2011 at 9:47

I found solution based on url_to_postid function

$url = explode('?', 'http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
$ID = url_to_postid($url[0]);

With this I can get post/page ID within the functions.php file.


This has nothing to do with it being in the functions file, but rather that the function is being called within 'The Loop' because it is attached to the excerpt filter. is_page() will always return false inside the loop, so it's not useful there.

  • No, it should work. is_page() checks the global $wp_query which stores the 'main query' and so can be used pretty much anywhere after the query has been set up. In fact 'the Loop' is called from within the template - so although the code might be in functions.php, it'll be triggered by the template. Commented Sep 27, 2012 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.