1

The file required doesn't load on single posts, it doesn't load anywhere! If I removed the is_single() function I can get it to work. My question is how I can check if the current page is a post?

I looked at other questions, it looks like I'm calling the function outside the loop but whatever I try in those answers don't work!!

How would you handle this? Thanks.

class Something {

    function my_func() {
        if ( is_single() ) {
            require_once( '/lib/control.php' );
        }
    }
}

$something = new Something();
$something->my_func();
1

At first, you're calling a WordPress function directly inside a new instance of your class which can lead to a null wp_query state which produces a log when calling your code.

To fix this you should move the call to a place somewhere like the init hook, like this:

class Something() {
    public function __construct() {
        add_action('init', [$this, 'my_func']);
    }
    public function my_func() {
        if ( is_single() ) {
            require_once( '/lib/control.php' );
        }
    }
}

new Something();

Your function will be run after the WordPress initialization is ended and before any headers so, the global $wp_query instance now knows what's happening.

If the above solution didn't work for you, You can also try these:

  1. Move your call inside the single.php file of your theme. (easy, messy)
  2. Double check if your single.php file had been wrapped with a loop.

  3. Try to replace is_single method with $wp_query->is_single() and see what happens. (don't forget to add global $wp_query at the first line of your function)

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.