• softplaza

    (@softplaza)


    I have,

    function my_function_init(){
        global $post;
        if(is_single()) {
            echo $post->ID;
        }
    }
    add_action('init', 'my_function_init');

    this doesn’t work for me, it doesn’t recognize if its a post even if its a post (is_single() = checks if its a post), i have this in the plugin i am writing for wordpress, please help

Viewing 2 replies - 1 through 2 (of 2 total)
  • First of all, try using is_singular(), then checking the post_type parameter of the global $post variable. It might be a little more reliable and is certainly more flexible than is_single().

    If you still can’t get it to work, try moving your function from the init action to the widgets_init action and see if it works, then. The codex article about conditional tags says:

    Warning: You can only use conditional query tags on or after the init action hook in WordPress.

    I’m not sure if that means it works as part of the init action, or if it needs to happen after the init action.

    Thread Starter softplaza

    (@softplaza)

    $post is null in my_function_init 🙁 what do i do?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘need help with wordpress add_action init to recognise is_single’ is closed to new replies.