0

I'm attempting to move some code from a plugin over to my theme's functions.php file, but when I attempt to wrap it in the is_single() wrapper inside of functions.php, it never fires. What am I missing?

if (is_single()) {
    echo "hello world"; 
}

I can't get the echo to fire from any inside post page.

This has to be something insanely simple I'm overlooking.

1 Answer 1

2

All that I can think of is to verify that you are in fact on a single post page ( echo out an else conditional, make sure that you are actually hitting your block of code ).

if ( is_single() ) {
    echo 'Hello World, Is Single';
} else {
    echo 'Not Single';
}

Where exactly are you placing the code (Header, Body, Footer, which template file)? Are you using any frameworks like Carrington ?

4
  • I'm calling is_single from inside of functions.php. I believe I have to place it inside a function and call the function from an add_action statement.
    – Scott B
    Jan 7, 2011 at 19:30
  • For example, this is the only way I can get it to work > add_action('template_redirect', '_my_function');
    – Scott B
    Jan 7, 2011 at 19:31
  • 1
    Yes, calling it after the template_redirect action is the only way it will work. If the plugin called it directly, it was doing it wrong.
    – scribu
    Jan 7, 2011 at 19:38
  • 1
    Just think about where in the script you are calling is_single(). If you add is_single to an an action ( eg template_redirect ) it will be called when that action is fired, make sure that the $wp_query object is available at that point. If you call is_single from a function that is on a template that is defined in your functions file you should be fine. Jan 7, 2011 at 19:45

Your Answer

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

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