How to get the current page id in PHP?

How to get the current page id in PHP?

You can get ID of the post in current page outside the loop using the technique below: Chosen answer is working only if you put it in the WordPress loop. Outside it will render useless. In your case you need to get ‘_GET’ and inside ‘post’… The code should look like this: Thanks for contributing an answer to Stack Overflow!

How to get current page id outside the loop?

How to get current page ID outside the loop? How to get current page ID outside the loop? you can do it by adding below code.it will give you current page id. Thanks for contributing an answer to WordPress Development Stack Exchange!

How to get current post ID in function?

I want to get current post id in this function to display selected dropdown value.. Note that $post or get_queried_object_id () do not work until the first query was fired. So this options are available only at the hook template_redirect and later.

How to get the current page id in WordPress?

get_the_ID(); or $post->ID; returns the current page or post id in WordPress. But you need to ensure that your post is saved in wordpress post table. Other wise you can’t get the id , simply because of it is not an entry in wordpress database.

How to set current page ” active ” in PHP?

I’m not saying this is the optimal method, but it works for me and it’s simple to implement. adding this: to my link it works perfectly, I only can’t make the child of a submenu working to make the parent active. The variable “active” is assigned with the corresponding item of each page.

How does the navigation menu work in PHP?

The navigational menu indicates which page the user is on by displaying the Page Two link in a different color and background. The down side? As a developer, you must remember to manually remove id=”currentpage” from one link to the next as you develop each page. Not so if you use PHP.

How can I keep my menu items current in PHP?

Adding a unique id or class attribute to indicate which menu item reflects a user’s current page, however, can become laborious. Even if you use body id attributes instead, as ALA does, some labor is involved and it is easy to make mistakes. But thanks to PHP, we can add these current-page indicators automatically.