WordPress Get Page ID Outside of Loop

I want to get the page id before starting the loop in WordPress. I use

$page = get_query_var('page_id'); 

Apparently, nothing returns.

I just want to check the page for its identifier and add a class to the <body> based on it.

+70
php wordpress
Jun 27 '10 at 12:57 on
source share
11 answers

If you use fairly permalinks, get_query_var('page_id') will not work.

Instead, get the id of the requested object from the global $wp_query :

 // Since 3.1 - recommended! $page_object = get_queried_object(); $page_id = get_queried_object_id(); // "Dirty" pre 3.1 global $wp_query; $page_object = $wp_query->get_queried_object(); $page_id = $wp_query->get_queried_object_id(); 
+149
Jun 27 '10 at 15:14
source share

You can also create a general function to get the message identifier, regardless of whether it is located outside or inside the loop (handles both cases):

 <?php /** * @uses WP_Query * @uses get_queried_object() * @see get_the_ID() * @return int */ function get_the_post_id() { if (in_the_loop()) { $post_id = get_the_ID(); } else { global $wp_query; $post_id = $wp_query->get_queried_object_id(); } return $post_id; } ?> 

And just do:

 $page_id = get_the_post_id(); 
+8
Sep 07 '14 at 10:44
source share

Use this global $ post instead:

 global $post; echo $post->ID; 
+6
Dec 08 2018-11-11T00:
source share

If you were looking for this topic in any way because of the message page (alternate page page when using a static first page), then the correct answer is:

 if (get_option('show_on_front') == 'page') { $page_id = get_option('page_for_posts'); echo get_the_title($page_id); } 

(taken from Forrst | Echo WordPress "Posts Pages" title - some code from tammyhart )

+4
Jul 11 '13 at 21:52
source share

You can use is_page($page_id) outside the loop to check.

0
Jun 27 '10 at 13:12
source share

This function gets the identifier from the current page.

 get_the_ID(); 
0
Mar 14 2018-12-12T00:
source share

Use below two lines of code to get the current page or message

 global $post; echo $post->ID; 
0
Jul 03 '17 at 14:08
source share

If you are on the page and this does not work:

 $page_object = get_queried_object(); $page_id = get_queried_object_id(); 

you can try creating a permalink manually using PHP so that you can find the post id:

 // get or make permalink $url = !empty(get_the_permalink()) ? get_the_permalink() : (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $permalink = strtok($url, '?'); // get post_id using url/permalink $post_id = url_to_postid($url); // want the post or postmeta? use get_post() or get_post_meta() $post = get_post($post_id); $postmeta = get_post_meta($post_id); 

It may not catch all possible permalinks (especially since I delete the query string), but you can change it according to your use case.

0
Oct 04 '17 at 17:16
source share

I did it as follows and it worked perfectly for me.

First, they declared a global variable in header.php, assigning the identifier of the message or page before changing it, since LOOP assigns the identifier of the last record shown:

$GLOBALS['pageid] = $wp_query->get_queried_object_id();

And use anywhere in the template, for example, in the footer.php file:

echo $GLOBALS['pageid];

0
Feb 26 '18 at 19:41
source share

This is the correct code.

 echo $post->ID; 
-3
Jun 14 '13 at 10:29
source share

If you are not using WordPress Loop, you cannot use any wordpress method, so you should use pure php.

You can use this code. And they will definitely help you :)

 $page_id = @$_GET['page_id']; if (!is_numeric($page_id)) { // Then the uri must be in friendly format aka /my_domain/category/onepage/ // Try this //$path = '/www/public_html/index.php/'; ///$path = '/my_domain/category/onepage/'; $path = $_SERVER['REQUEST_URI']; // Clean the uri //$path = str_replace('/', '', $page); $path = str_replace('.php', '', $path); //$path = str_replace('?s=', '', $path); $path = $path ? $path : 'default'; $path_len = strlen($path); $last_char = substr($path, $path_len -1); //echo $last_char; $has_slash = strpos($last_char, "/"); //echo $has_slash; if ($has_slash === 0) : $path = substr($path, 0, $path_len -1); elseif ($has_slash === null) : $path = substr($path, 0, $path_len); endif; //echo "path: ".$path; // '/www/public_html/index' $page = substr(strrchr($path, "/"), 1); echo "page: ".$page; // 'index' } $my_page_id = 31; $my_page = 'mypage'; //echo "page: ".$page; //echo "page_id ".$page_id; if($page_id == $my_page_id || $page == $my_page) { // your stuff.... } 

Enjoy it!

-3
Feb 12 '14 at 9:13
source share



All Articles