Search
Close this search box.

How to Get the Current Page ID Inside a Widget

Here’s a quick tip on how to get the current page ID inside a WordPress widget (outside of The Loop).

With WordPress sometimes you’re not always coding inside The Loop.

Using this little hack allows your widgets to grab the current page ID and could be used to set contextual information which isn’t always easy in WordPress.

Of course it can be used in other parts of your WordPress template too such as footers and asides.

[pastacode lang=”php” manual=”%2F%2F%20Get%20Current%20Page%20ID%20outside%20Loop%0Aglobal%20%24wp_query%3B%0A%24post_obj%20%3D%20%24wp_query-%3Eget_queried_object()%3B%0A%24Page_ID%20%3D%20%24post_obj-%3EID%3B%0Aecho%20%24Page_ID%3B” message=”” highlight=”” provider=”manual”/]

Was this article helpful?
YesNo