WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to get the current page in functions.php (3 posts)

  1. remino
    Member
    Posted 6 years ago #

    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?

  2. Kafkaesqui

    Posted 6 years ago #

    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).

  3. remino
    Member
    Posted 6 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic