Search This Blog

Wednesday, 9 January 2013

Get the post / page ID in functions.php

Usually the global variable $post will give the user all the information of the current post / page. This global variable is not available in functions.php (if the code is not in a function) so you can use the following code snippet to get the ID of the post/page ID based on current URL.

$currenturl = explode('?', 'http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
$curpostId = url_to_postid($currenturl [0]);




$curpostId value will be zero if the current page is not a post / page.

No comments:

Post a Comment