WordPress.org

Support

Support » Plugins and Hacks » [Resolved] How to get the current page in functions.php

[Resolved] How to get the current page in functions.php

  • I’m writing a theme, and I’m trying to get the current page in a function in functions.php.

    I tried is_page($post->ID), is_page($post->post_name), is_single($post->ID), and $wp_query->get_queried_object_id(), but I always get false or 0 (integer) on any page I go to, even the home page.

    Any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • 1. Are you scoping $post or $wp_query to global before using them?

    2. What are you hooking into to run your function? Running it before a query actually occurs would of course always return false. Instead you want to hook into a plugin API event (such as a template API hook, but it would depend on what your function does to decide on the best one).

    Ah, of course. You made me realise the mistake I’ve done. I wrote a function to replace wp_list_pages() and I needed to get the current page. To test that function, I actually called it from within functions.php. When I’m calling my custom function from index.php (the main template page), every works just fine.

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] How to get the current page in functions.php’ is closed to new replies.