0

When is code in functions.php executed? Is it during the loop? The following does not work when placed in functions.php (not within a function) :

if (is_page( 27 )) {

echo 'this is the apply page';

}

The codex specifies that is_page() cannot run within the loop.

4

is_page() relies on a complete global $wp_query object. If you call it before the action template_redirect has been fired, it might be impossible to get that data.

Wait for a proper action.

Example:

add_filter( 'template_include', function( $template ) {
    if ( is_page( 27 ) )
        echo 'this is the apply page';

    return $template;
});

You can call is_page() in a loop too.

  • Thanks. Did you mean 'template_redirect' instead of 'template_include' as the first argument to add_action? If you can call is_page() in the loop why does the codex say you can't? Just out of date? If so, is there a better reference? – codecowboy Dec 12 '14 at 12:23
  • @codecowboy You can use both hooks or any other hook later. The codex is full of questionable information. The only reliable source is the WordPress code. – fuxia Dec 12 '14 at 12:28
0

First you need to call page ID via get_the_ID(); function, then you can use is_page function like this:

    $id = get_the_ID();

    if ( is_page( $id == 2591 ) )
    {
    return __( 'Add to cart', 'woocommerce' );
    }
    else
    {
    return __( 'BUY NOW', 'woocommerce' );
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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