WordPress Get the Page ID outside the loop
Multi tool use
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I want to get the page ID before starting the loop in WordPress. I am using
$page = get_query_var('page_id');
Apparently, it returns nothing.
I just want to check a page for its ID and add a class to <body>
tag based on it.
php wordpress
add a comment |
I want to get the page ID before starting the loop in WordPress. I am using
$page = get_query_var('page_id');
Apparently, it returns nothing.
I just want to check a page for its ID and add a class to <body>
tag based on it.
php wordpress
3
stackoverflow.com/questions/22351038/…
– user7118434
Dec 17 '16 at 13:27
add a comment |
I want to get the page ID before starting the loop in WordPress. I am using
$page = get_query_var('page_id');
Apparently, it returns nothing.
I just want to check a page for its ID and add a class to <body>
tag based on it.
php wordpress
I want to get the page ID before starting the loop in WordPress. I am using
$page = get_query_var('page_id');
Apparently, it returns nothing.
I just want to check a page for its ID and add a class to <body>
tag based on it.
php wordpress
php wordpress
edited 2 days ago
Maxime
5,56313747
5,56313747
asked Jun 27 '10 at 12:57
Atif Mohammed AmeenuddinAtif Mohammed Ameenuddin
5,823165694
5,823165694
3
stackoverflow.com/questions/22351038/…
– user7118434
Dec 17 '16 at 13:27
add a comment |
3
stackoverflow.com/questions/22351038/…
– user7118434
Dec 17 '16 at 13:27
3
3
stackoverflow.com/questions/22351038/…
– user7118434
Dec 17 '16 at 13:27
stackoverflow.com/questions/22351038/…
– user7118434
Dec 17 '16 at 13:27
add a comment |
11 Answers
11
active
oldest
votes
If you're using pretty permalinks, get_query_var('page_id')
won't work.
Instead, get the queried object ID 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();
Perfect for pretty permalinks. I Used global $post; echo $post->ID; But not worked that. Thanks!
– Sumith Harshan
Sep 28 '13 at 8:43
6
get_queried_object_id();
return 0 for me. I think that the problem is that i'm calling it after a custom query. I want de actual page Id.
– Victor
May 23 '14 at 9:26
Strange,get_queried_object();
didn't work for me, but$wp_query->get_queried_object();
does... I took a look at theget_queried_object();
and it is the same as doing the latter.
– SeanJA
Sep 24 '14 at 13:48
Might be your variable scope - have you overridden$wp_query
with a custom query?
– TheDeadMedic
Sep 25 '14 at 13:22
add a comment |
You can also create a generic function to get the ID of the post, whether its outside or inside the loop (handles both the 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 simply do:
$page_id = get_the_post_id();
does this actually work? it's either an infinitely recursive function, or it'll break becauseget_the_ID
is an already declared function. i think you meant to change the function name, or this was part of a class?
– drzaus
Apr 21 '15 at 18:21
@drzaus thanks for pointing that out. I fixed it.
– Nadeem Khan
Mar 25 '16 at 7:35
add a comment |
Use this global $post instead:
global $post;
echo $post->ID;
This will only work after the loop, not before, since$post
is initialized when starting "the loop".
– Christian Davén
May 28 '13 at 8:32
6
@ChristianDavén - this is not true. This code works on beginning of the page.php
– iWizard
Jun 14 '13 at 10:31
add a comment |
If you by any means searched this topic because of the post page (index page alternative when using static front page), then the right answer is this:
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 Page" title - Some code from tammyhart)
add a comment |
You can use is_page($page_id)
outside the loop to check.
I dont want to check a page, I want to get the ID of current page.
– Atif Mohammed Ameenuddin
Jun 27 '10 at 13:31
@atif are you sure a page ID is in fact being passed? You don't happen to be on the front page?
– Pekka 웃
Jun 27 '10 at 13:57
yes I did check it
– Atif Mohammed Ameenuddin
Jun 27 '10 at 18:50
add a comment |
This function get id off a page current.
get_the_ID();
4
um...this only works if you're in the loop: Returns the numeric ID of the current post. This tag must be within The Loop.
– drzaus
Mar 1 '13 at 22:18
@drzaus Actually this does work outside the loop... Check it out.
– hitautodestruct
Apr 19 '15 at 6:34
1
@hitautodestruct while you are technically correct that it could work outside the loop, it's not a reliable usage -- this is from personal experience as well looking at the source code. The underlying method get_post happens to use$GLOBALS['post']
, which could have been populated at some point but there's no guarantee unless/until you're in the loop.
– drzaus
Apr 20 '15 at 16:35
@drzaus point taken. Thanks for clarifying that :)
– hitautodestruct
Apr 20 '15 at 16:36
stackoverflow.com/questions/22351038/…
– user7118434
Dec 14 '16 at 7:14
add a comment |
Use below two lines of code to get current page or post ID
global $post;
echo $post->ID;
add a comment |
If you're on a page and this does not work:
$page_object = get_queried_object();
$page_id = get_queried_object_id();
you can try to build the permalink manually with PHP so you can lookup 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 every possible permalink (especially since I'm stripping out the query string), but you can modify it to fit your use case.
add a comment |
I have done it in the following way and it has worked perfectly for me.
First declared a global variable in the header.php, assigning the ID of the post or page before it changes, since the LOOP assigns it the ID of the last entry shown:
$GLOBALS['pageid] = $wp_query->get_queried_object_id();
And to use anywhere in the template, example in the footer.php:
echo $GLOBALS['pageid];
add a comment |
This is the correct code.
echo $post->ID;
add a comment |
If you are out of the Loop of WordPress you can not use any of the method of wordpress so you must use pure php.
You can use this code. And sure will 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!
2
Very wrong in all ways.
– JakeParis
Jul 30 '14 at 20:13
Maybe.. Could you please give more details about this and show me your solution?
– edcv
Nov 10 '14 at 19:10
1
you wrote 50 lines of code to get the variable that already exists in$post->ID
. Even if you're not in the loop, you can use many, many Wordpress functions. Just not the few that must be used in the loop.
– JakeParis
Nov 10 '14 at 21:49
Well if you remove the commented code, i wrote 20 lines. Those lines saved my day in the meantine process of learning wordpress. You wrote 3 lines but you don't apport any solution to the OP question when you are outside the loop.
– edcv
Nov 14 '14 at 17:37
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3127385%2fwordpress-get-the-page-id-outside-the-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you're using pretty permalinks, get_query_var('page_id')
won't work.
Instead, get the queried object ID 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();
Perfect for pretty permalinks. I Used global $post; echo $post->ID; But not worked that. Thanks!
– Sumith Harshan
Sep 28 '13 at 8:43
6
get_queried_object_id();
return 0 for me. I think that the problem is that i'm calling it after a custom query. I want de actual page Id.
– Victor
May 23 '14 at 9:26
Strange,get_queried_object();
didn't work for me, but$wp_query->get_queried_object();
does... I took a look at theget_queried_object();
and it is the same as doing the latter.
– SeanJA
Sep 24 '14 at 13:48
Might be your variable scope - have you overridden$wp_query
with a custom query?
– TheDeadMedic
Sep 25 '14 at 13:22
add a comment |
If you're using pretty permalinks, get_query_var('page_id')
won't work.
Instead, get the queried object ID 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();
Perfect for pretty permalinks. I Used global $post; echo $post->ID; But not worked that. Thanks!
– Sumith Harshan
Sep 28 '13 at 8:43
6
get_queried_object_id();
return 0 for me. I think that the problem is that i'm calling it after a custom query. I want de actual page Id.
– Victor
May 23 '14 at 9:26
Strange,get_queried_object();
didn't work for me, but$wp_query->get_queried_object();
does... I took a look at theget_queried_object();
and it is the same as doing the latter.
– SeanJA
Sep 24 '14 at 13:48
Might be your variable scope - have you overridden$wp_query
with a custom query?
– TheDeadMedic
Sep 25 '14 at 13:22
add a comment |
If you're using pretty permalinks, get_query_var('page_id')
won't work.
Instead, get the queried object ID 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();
If you're using pretty permalinks, get_query_var('page_id')
won't work.
Instead, get the queried object ID 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();
edited Feb 2 '13 at 1:52
answered Jun 27 '10 at 15:14
TheDeadMedicTheDeadMedic
8,93423046
8,93423046
Perfect for pretty permalinks. I Used global $post; echo $post->ID; But not worked that. Thanks!
– Sumith Harshan
Sep 28 '13 at 8:43
6
get_queried_object_id();
return 0 for me. I think that the problem is that i'm calling it after a custom query. I want de actual page Id.
– Victor
May 23 '14 at 9:26
Strange,get_queried_object();
didn't work for me, but$wp_query->get_queried_object();
does... I took a look at theget_queried_object();
and it is the same as doing the latter.
– SeanJA
Sep 24 '14 at 13:48
Might be your variable scope - have you overridden$wp_query
with a custom query?
– TheDeadMedic
Sep 25 '14 at 13:22
add a comment |
Perfect for pretty permalinks. I Used global $post; echo $post->ID; But not worked that. Thanks!
– Sumith Harshan
Sep 28 '13 at 8:43
6
get_queried_object_id();
return 0 for me. I think that the problem is that i'm calling it after a custom query. I want de actual page Id.
– Victor
May 23 '14 at 9:26
Strange,get_queried_object();
didn't work for me, but$wp_query->get_queried_object();
does... I took a look at theget_queried_object();
and it is the same as doing the latter.
– SeanJA
Sep 24 '14 at 13:48
Might be your variable scope - have you overridden$wp_query
with a custom query?
– TheDeadMedic
Sep 25 '14 at 13:22
Perfect for pretty permalinks. I Used global $post; echo $post->ID; But not worked that. Thanks!
– Sumith Harshan
Sep 28 '13 at 8:43
Perfect for pretty permalinks. I Used global $post; echo $post->ID; But not worked that. Thanks!
– Sumith Harshan
Sep 28 '13 at 8:43
6
6
get_queried_object_id();
return 0 for me. I think that the problem is that i'm calling it after a custom query. I want de actual page Id.– Victor
May 23 '14 at 9:26
get_queried_object_id();
return 0 for me. I think that the problem is that i'm calling it after a custom query. I want de actual page Id.– Victor
May 23 '14 at 9:26
Strange,
get_queried_object();
didn't work for me, but $wp_query->get_queried_object();
does... I took a look at the get_queried_object();
and it is the same as doing the latter.– SeanJA
Sep 24 '14 at 13:48
Strange,
get_queried_object();
didn't work for me, but $wp_query->get_queried_object();
does... I took a look at the get_queried_object();
and it is the same as doing the latter.– SeanJA
Sep 24 '14 at 13:48
Might be your variable scope - have you overridden
$wp_query
with a custom query?– TheDeadMedic
Sep 25 '14 at 13:22
Might be your variable scope - have you overridden
$wp_query
with a custom query?– TheDeadMedic
Sep 25 '14 at 13:22
add a comment |
You can also create a generic function to get the ID of the post, whether its outside or inside the loop (handles both the 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 simply do:
$page_id = get_the_post_id();
does this actually work? it's either an infinitely recursive function, or it'll break becauseget_the_ID
is an already declared function. i think you meant to change the function name, or this was part of a class?
– drzaus
Apr 21 '15 at 18:21
@drzaus thanks for pointing that out. I fixed it.
– Nadeem Khan
Mar 25 '16 at 7:35
add a comment |
You can also create a generic function to get the ID of the post, whether its outside or inside the loop (handles both the 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 simply do:
$page_id = get_the_post_id();
does this actually work? it's either an infinitely recursive function, or it'll break becauseget_the_ID
is an already declared function. i think you meant to change the function name, or this was part of a class?
– drzaus
Apr 21 '15 at 18:21
@drzaus thanks for pointing that out. I fixed it.
– Nadeem Khan
Mar 25 '16 at 7:35
add a comment |
You can also create a generic function to get the ID of the post, whether its outside or inside the loop (handles both the 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 simply do:
$page_id = get_the_post_id();
You can also create a generic function to get the ID of the post, whether its outside or inside the loop (handles both the 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 simply do:
$page_id = get_the_post_id();
edited Mar 25 '16 at 7:34
answered Sep 7 '14 at 10:44
Nadeem KhanNadeem Khan
2,67912034
2,67912034
does this actually work? it's either an infinitely recursive function, or it'll break becauseget_the_ID
is an already declared function. i think you meant to change the function name, or this was part of a class?
– drzaus
Apr 21 '15 at 18:21
@drzaus thanks for pointing that out. I fixed it.
– Nadeem Khan
Mar 25 '16 at 7:35
add a comment |
does this actually work? it's either an infinitely recursive function, or it'll break becauseget_the_ID
is an already declared function. i think you meant to change the function name, or this was part of a class?
– drzaus
Apr 21 '15 at 18:21
@drzaus thanks for pointing that out. I fixed it.
– Nadeem Khan
Mar 25 '16 at 7:35
does this actually work? it's either an infinitely recursive function, or it'll break because
get_the_ID
is an already declared function. i think you meant to change the function name, or this was part of a class?– drzaus
Apr 21 '15 at 18:21
does this actually work? it's either an infinitely recursive function, or it'll break because
get_the_ID
is an already declared function. i think you meant to change the function name, or this was part of a class?– drzaus
Apr 21 '15 at 18:21
@drzaus thanks for pointing that out. I fixed it.
– Nadeem Khan
Mar 25 '16 at 7:35
@drzaus thanks for pointing that out. I fixed it.
– Nadeem Khan
Mar 25 '16 at 7:35
add a comment |
Use this global $post instead:
global $post;
echo $post->ID;
This will only work after the loop, not before, since$post
is initialized when starting "the loop".
– Christian Davén
May 28 '13 at 8:32
6
@ChristianDavén - this is not true. This code works on beginning of the page.php
– iWizard
Jun 14 '13 at 10:31
add a comment |
Use this global $post instead:
global $post;
echo $post->ID;
This will only work after the loop, not before, since$post
is initialized when starting "the loop".
– Christian Davén
May 28 '13 at 8:32
6
@ChristianDavén - this is not true. This code works on beginning of the page.php
– iWizard
Jun 14 '13 at 10:31
add a comment |
Use this global $post instead:
global $post;
echo $post->ID;
Use this global $post instead:
global $post;
echo $post->ID;
edited Jan 18 '16 at 23:07
Marc
2,33612643
2,33612643
answered Dec 8 '11 at 5:51
ryscriptryscript
10111
10111
This will only work after the loop, not before, since$post
is initialized when starting "the loop".
– Christian Davén
May 28 '13 at 8:32
6
@ChristianDavén - this is not true. This code works on beginning of the page.php
– iWizard
Jun 14 '13 at 10:31
add a comment |
This will only work after the loop, not before, since$post
is initialized when starting "the loop".
– Christian Davén
May 28 '13 at 8:32
6
@ChristianDavén - this is not true. This code works on beginning of the page.php
– iWizard
Jun 14 '13 at 10:31
This will only work after the loop, not before, since
$post
is initialized when starting "the loop".– Christian Davén
May 28 '13 at 8:32
This will only work after the loop, not before, since
$post
is initialized when starting "the loop".– Christian Davén
May 28 '13 at 8:32
6
6
@ChristianDavén - this is not true. This code works on beginning of the page.php
– iWizard
Jun 14 '13 at 10:31
@ChristianDavén - this is not true. This code works on beginning of the page.php
– iWizard
Jun 14 '13 at 10:31
add a comment |
If you by any means searched this topic because of the post page (index page alternative when using static front page), then the right answer is this:
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 Page" title - Some code from tammyhart)
add a comment |
If you by any means searched this topic because of the post page (index page alternative when using static front page), then the right answer is this:
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 Page" title - Some code from tammyhart)
add a comment |
If you by any means searched this topic because of the post page (index page alternative when using static front page), then the right answer is this:
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 Page" title - Some code from tammyhart)
If you by any means searched this topic because of the post page (index page alternative when using static front page), then the right answer is this:
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 Page" title - Some code from tammyhart)
edited Jan 6 '14 at 15:26
blade19899
4171527
4171527
answered Jul 11 '13 at 21:52
banestobanesto
1,27811113
1,27811113
add a comment |
add a comment |
You can use is_page($page_id)
outside the loop to check.
I dont want to check a page, I want to get the ID of current page.
– Atif Mohammed Ameenuddin
Jun 27 '10 at 13:31
@atif are you sure a page ID is in fact being passed? You don't happen to be on the front page?
– Pekka 웃
Jun 27 '10 at 13:57
yes I did check it
– Atif Mohammed Ameenuddin
Jun 27 '10 at 18:50
add a comment |
You can use is_page($page_id)
outside the loop to check.
I dont want to check a page, I want to get the ID of current page.
– Atif Mohammed Ameenuddin
Jun 27 '10 at 13:31
@atif are you sure a page ID is in fact being passed? You don't happen to be on the front page?
– Pekka 웃
Jun 27 '10 at 13:57
yes I did check it
– Atif Mohammed Ameenuddin
Jun 27 '10 at 18:50
add a comment |
You can use is_page($page_id)
outside the loop to check.
You can use is_page($page_id)
outside the loop to check.
answered Jun 27 '10 at 13:12
nikc.orgnikc.org
12.5k23676
12.5k23676
I dont want to check a page, I want to get the ID of current page.
– Atif Mohammed Ameenuddin
Jun 27 '10 at 13:31
@atif are you sure a page ID is in fact being passed? You don't happen to be on the front page?
– Pekka 웃
Jun 27 '10 at 13:57
yes I did check it
– Atif Mohammed Ameenuddin
Jun 27 '10 at 18:50
add a comment |
I dont want to check a page, I want to get the ID of current page.
– Atif Mohammed Ameenuddin
Jun 27 '10 at 13:31
@atif are you sure a page ID is in fact being passed? You don't happen to be on the front page?
– Pekka 웃
Jun 27 '10 at 13:57
yes I did check it
– Atif Mohammed Ameenuddin
Jun 27 '10 at 18:50
I dont want to check a page, I want to get the ID of current page.
– Atif Mohammed Ameenuddin
Jun 27 '10 at 13:31
I dont want to check a page, I want to get the ID of current page.
– Atif Mohammed Ameenuddin
Jun 27 '10 at 13:31
@atif are you sure a page ID is in fact being passed? You don't happen to be on the front page?
– Pekka 웃
Jun 27 '10 at 13:57
@atif are you sure a page ID is in fact being passed? You don't happen to be on the front page?
– Pekka 웃
Jun 27 '10 at 13:57
yes I did check it
– Atif Mohammed Ameenuddin
Jun 27 '10 at 18:50
yes I did check it
– Atif Mohammed Ameenuddin
Jun 27 '10 at 18:50
add a comment |
This function get id off a page current.
get_the_ID();
4
um...this only works if you're in the loop: Returns the numeric ID of the current post. This tag must be within The Loop.
– drzaus
Mar 1 '13 at 22:18
@drzaus Actually this does work outside the loop... Check it out.
– hitautodestruct
Apr 19 '15 at 6:34
1
@hitautodestruct while you are technically correct that it could work outside the loop, it's not a reliable usage -- this is from personal experience as well looking at the source code. The underlying method get_post happens to use$GLOBALS['post']
, which could have been populated at some point but there's no guarantee unless/until you're in the loop.
– drzaus
Apr 20 '15 at 16:35
@drzaus point taken. Thanks for clarifying that :)
– hitautodestruct
Apr 20 '15 at 16:36
stackoverflow.com/questions/22351038/…
– user7118434
Dec 14 '16 at 7:14
add a comment |
This function get id off a page current.
get_the_ID();
4
um...this only works if you're in the loop: Returns the numeric ID of the current post. This tag must be within The Loop.
– drzaus
Mar 1 '13 at 22:18
@drzaus Actually this does work outside the loop... Check it out.
– hitautodestruct
Apr 19 '15 at 6:34
1
@hitautodestruct while you are technically correct that it could work outside the loop, it's not a reliable usage -- this is from personal experience as well looking at the source code. The underlying method get_post happens to use$GLOBALS['post']
, which could have been populated at some point but there's no guarantee unless/until you're in the loop.
– drzaus
Apr 20 '15 at 16:35
@drzaus point taken. Thanks for clarifying that :)
– hitautodestruct
Apr 20 '15 at 16:36
stackoverflow.com/questions/22351038/…
– user7118434
Dec 14 '16 at 7:14
add a comment |
This function get id off a page current.
get_the_ID();
This function get id off a page current.
get_the_ID();
answered Mar 14 '12 at 15:13
jruzafajruzafa
3,47811725
3,47811725
4
um...this only works if you're in the loop: Returns the numeric ID of the current post. This tag must be within The Loop.
– drzaus
Mar 1 '13 at 22:18
@drzaus Actually this does work outside the loop... Check it out.
– hitautodestruct
Apr 19 '15 at 6:34
1
@hitautodestruct while you are technically correct that it could work outside the loop, it's not a reliable usage -- this is from personal experience as well looking at the source code. The underlying method get_post happens to use$GLOBALS['post']
, which could have been populated at some point but there's no guarantee unless/until you're in the loop.
– drzaus
Apr 20 '15 at 16:35
@drzaus point taken. Thanks for clarifying that :)
– hitautodestruct
Apr 20 '15 at 16:36
stackoverflow.com/questions/22351038/…
– user7118434
Dec 14 '16 at 7:14
add a comment |
4
um...this only works if you're in the loop: Returns the numeric ID of the current post. This tag must be within The Loop.
– drzaus
Mar 1 '13 at 22:18
@drzaus Actually this does work outside the loop... Check it out.
– hitautodestruct
Apr 19 '15 at 6:34
1
@hitautodestruct while you are technically correct that it could work outside the loop, it's not a reliable usage -- this is from personal experience as well looking at the source code. The underlying method get_post happens to use$GLOBALS['post']
, which could have been populated at some point but there's no guarantee unless/until you're in the loop.
– drzaus
Apr 20 '15 at 16:35
@drzaus point taken. Thanks for clarifying that :)
– hitautodestruct
Apr 20 '15 at 16:36
stackoverflow.com/questions/22351038/…
– user7118434
Dec 14 '16 at 7:14
4
4
um...this only works if you're in the loop: Returns the numeric ID of the current post. This tag must be within The Loop.
– drzaus
Mar 1 '13 at 22:18
um...this only works if you're in the loop: Returns the numeric ID of the current post. This tag must be within The Loop.
– drzaus
Mar 1 '13 at 22:18
@drzaus Actually this does work outside the loop... Check it out.
– hitautodestruct
Apr 19 '15 at 6:34
@drzaus Actually this does work outside the loop... Check it out.
– hitautodestruct
Apr 19 '15 at 6:34
1
1
@hitautodestruct while you are technically correct that it could work outside the loop, it's not a reliable usage -- this is from personal experience as well looking at the source code. The underlying method get_post happens to use
$GLOBALS['post']
, which could have been populated at some point but there's no guarantee unless/until you're in the loop.– drzaus
Apr 20 '15 at 16:35
@hitautodestruct while you are technically correct that it could work outside the loop, it's not a reliable usage -- this is from personal experience as well looking at the source code. The underlying method get_post happens to use
$GLOBALS['post']
, which could have been populated at some point but there's no guarantee unless/until you're in the loop.– drzaus
Apr 20 '15 at 16:35
@drzaus point taken. Thanks for clarifying that :)
– hitautodestruct
Apr 20 '15 at 16:36
@drzaus point taken. Thanks for clarifying that :)
– hitautodestruct
Apr 20 '15 at 16:36
stackoverflow.com/questions/22351038/…
– user7118434
Dec 14 '16 at 7:14
stackoverflow.com/questions/22351038/…
– user7118434
Dec 14 '16 at 7:14
add a comment |
Use below two lines of code to get current page or post ID
global $post;
echo $post->ID;
add a comment |
Use below two lines of code to get current page or post ID
global $post;
echo $post->ID;
add a comment |
Use below two lines of code to get current page or post ID
global $post;
echo $post->ID;
Use below two lines of code to get current page or post ID
global $post;
echo $post->ID;
answered Jul 3 '17 at 14:08
Braj Kishor SahBraj Kishor Sah
6415
6415
add a comment |
add a comment |
If you're on a page and this does not work:
$page_object = get_queried_object();
$page_id = get_queried_object_id();
you can try to build the permalink manually with PHP so you can lookup 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 every possible permalink (especially since I'm stripping out the query string), but you can modify it to fit your use case.
add a comment |
If you're on a page and this does not work:
$page_object = get_queried_object();
$page_id = get_queried_object_id();
you can try to build the permalink manually with PHP so you can lookup 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 every possible permalink (especially since I'm stripping out the query string), but you can modify it to fit your use case.
add a comment |
If you're on a page and this does not work:
$page_object = get_queried_object();
$page_id = get_queried_object_id();
you can try to build the permalink manually with PHP so you can lookup 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 every possible permalink (especially since I'm stripping out the query string), but you can modify it to fit your use case.
If you're on a page and this does not work:
$page_object = get_queried_object();
$page_id = get_queried_object_id();
you can try to build the permalink manually with PHP so you can lookup 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 every possible permalink (especially since I'm stripping out the query string), but you can modify it to fit your use case.
edited Oct 4 '17 at 17:35
answered Oct 4 '17 at 17:16
Jarrett BarnettJarrett Barnett
62039
62039
add a comment |
add a comment |
I have done it in the following way and it has worked perfectly for me.
First declared a global variable in the header.php, assigning the ID of the post or page before it changes, since the LOOP assigns it the ID of the last entry shown:
$GLOBALS['pageid] = $wp_query->get_queried_object_id();
And to use anywhere in the template, example in the footer.php:
echo $GLOBALS['pageid];
add a comment |
I have done it in the following way and it has worked perfectly for me.
First declared a global variable in the header.php, assigning the ID of the post or page before it changes, since the LOOP assigns it the ID of the last entry shown:
$GLOBALS['pageid] = $wp_query->get_queried_object_id();
And to use anywhere in the template, example in the footer.php:
echo $GLOBALS['pageid];
add a comment |
I have done it in the following way and it has worked perfectly for me.
First declared a global variable in the header.php, assigning the ID of the post or page before it changes, since the LOOP assigns it the ID of the last entry shown:
$GLOBALS['pageid] = $wp_query->get_queried_object_id();
And to use anywhere in the template, example in the footer.php:
echo $GLOBALS['pageid];
I have done it in the following way and it has worked perfectly for me.
First declared a global variable in the header.php, assigning the ID of the post or page before it changes, since the LOOP assigns it the ID of the last entry shown:
$GLOBALS['pageid] = $wp_query->get_queried_object_id();
And to use anywhere in the template, example in the footer.php:
echo $GLOBALS['pageid];
answered Feb 26 '18 at 19:41
EddEdd
287
287
add a comment |
add a comment |
This is the correct code.
echo $post->ID;
add a comment |
This is the correct code.
echo $post->ID;
add a comment |
This is the correct code.
echo $post->ID;
This is the correct code.
echo $post->ID;
edited Jun 14 '13 at 10:30
Taryn♦
193k47295358
193k47295358
answered Jun 14 '13 at 10:29
BannaBanna
11
11
add a comment |
add a comment |
If you are out of the Loop of WordPress you can not use any of the method of wordpress so you must use pure php.
You can use this code. And sure will 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!
2
Very wrong in all ways.
– JakeParis
Jul 30 '14 at 20:13
Maybe.. Could you please give more details about this and show me your solution?
– edcv
Nov 10 '14 at 19:10
1
you wrote 50 lines of code to get the variable that already exists in$post->ID
. Even if you're not in the loop, you can use many, many Wordpress functions. Just not the few that must be used in the loop.
– JakeParis
Nov 10 '14 at 21:49
Well if you remove the commented code, i wrote 20 lines. Those lines saved my day in the meantine process of learning wordpress. You wrote 3 lines but you don't apport any solution to the OP question when you are outside the loop.
– edcv
Nov 14 '14 at 17:37
add a comment |
If you are out of the Loop of WordPress you can not use any of the method of wordpress so you must use pure php.
You can use this code. And sure will 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!
2
Very wrong in all ways.
– JakeParis
Jul 30 '14 at 20:13
Maybe.. Could you please give more details about this and show me your solution?
– edcv
Nov 10 '14 at 19:10
1
you wrote 50 lines of code to get the variable that already exists in$post->ID
. Even if you're not in the loop, you can use many, many Wordpress functions. Just not the few that must be used in the loop.
– JakeParis
Nov 10 '14 at 21:49
Well if you remove the commented code, i wrote 20 lines. Those lines saved my day in the meantine process of learning wordpress. You wrote 3 lines but you don't apport any solution to the OP question when you are outside the loop.
– edcv
Nov 14 '14 at 17:37
add a comment |
If you are out of the Loop of WordPress you can not use any of the method of wordpress so you must use pure php.
You can use this code. And sure will 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!
If you are out of the Loop of WordPress you can not use any of the method of wordpress so you must use pure php.
You can use this code. And sure will 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!
edited Feb 12 '14 at 9:53
answered Feb 12 '14 at 9:13
edcvedcv
232
232
2
Very wrong in all ways.
– JakeParis
Jul 30 '14 at 20:13
Maybe.. Could you please give more details about this and show me your solution?
– edcv
Nov 10 '14 at 19:10
1
you wrote 50 lines of code to get the variable that already exists in$post->ID
. Even if you're not in the loop, you can use many, many Wordpress functions. Just not the few that must be used in the loop.
– JakeParis
Nov 10 '14 at 21:49
Well if you remove the commented code, i wrote 20 lines. Those lines saved my day in the meantine process of learning wordpress. You wrote 3 lines but you don't apport any solution to the OP question when you are outside the loop.
– edcv
Nov 14 '14 at 17:37
add a comment |
2
Very wrong in all ways.
– JakeParis
Jul 30 '14 at 20:13
Maybe.. Could you please give more details about this and show me your solution?
– edcv
Nov 10 '14 at 19:10
1
you wrote 50 lines of code to get the variable that already exists in$post->ID
. Even if you're not in the loop, you can use many, many Wordpress functions. Just not the few that must be used in the loop.
– JakeParis
Nov 10 '14 at 21:49
Well if you remove the commented code, i wrote 20 lines. Those lines saved my day in the meantine process of learning wordpress. You wrote 3 lines but you don't apport any solution to the OP question when you are outside the loop.
– edcv
Nov 14 '14 at 17:37
2
2
Very wrong in all ways.
– JakeParis
Jul 30 '14 at 20:13
Very wrong in all ways.
– JakeParis
Jul 30 '14 at 20:13
Maybe.. Could you please give more details about this and show me your solution?
– edcv
Nov 10 '14 at 19:10
Maybe.. Could you please give more details about this and show me your solution?
– edcv
Nov 10 '14 at 19:10
1
1
you wrote 50 lines of code to get the variable that already exists in
$post->ID
. Even if you're not in the loop, you can use many, many Wordpress functions. Just not the few that must be used in the loop.– JakeParis
Nov 10 '14 at 21:49
you wrote 50 lines of code to get the variable that already exists in
$post->ID
. Even if you're not in the loop, you can use many, many Wordpress functions. Just not the few that must be used in the loop.– JakeParis
Nov 10 '14 at 21:49
Well if you remove the commented code, i wrote 20 lines. Those lines saved my day in the meantine process of learning wordpress. You wrote 3 lines but you don't apport any solution to the OP question when you are outside the loop.
– edcv
Nov 14 '14 at 17:37
Well if you remove the commented code, i wrote 20 lines. Those lines saved my day in the meantine process of learning wordpress. You wrote 3 lines but you don't apport any solution to the OP question when you are outside the loop.
– edcv
Nov 14 '14 at 17:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3127385%2fwordpress-get-the-page-id-outside-the-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
uzph4Fvscqwb8xNkjM7J7 Y0vLbXFiC
3
stackoverflow.com/questions/22351038/…
– user7118434
Dec 17 '16 at 13:27