fbpx

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.

// Get Current Page ID outside Loop
global $wp_query;
$post_obj = $wp_query->get_queried_object();
$Page_ID = $post_obj->ID;
echo $Page_ID;

Keep In Touch

Wil

Wil is a dad, consultant, developer, conference organiser, speaker and business mentor. He co-organizes the WordPress Sydney meetup group and has been on the orgnising committee for WordCamp Sydney since 2014. He speaks at many technical events and contributes to the WordPress open source project. His likes are chillies, craft beer and electrogravitics.

1 thought on “How to Get the Current Page ID Inside a Widget”

Comments are closed.

If You Enjoyed This Post
Join Our Newsletter
Give it a try, you can unsubscribe anytime.
Close