WordPress Get the Page ID outside the loop

Multi tool use
Multi tool use





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







70















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.










share|improve this question




















  • 3





    stackoverflow.com/questions/22351038/…

    – user7118434
    Dec 17 '16 at 13:27


















70















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.










share|improve this question




















  • 3





    stackoverflow.com/questions/22351038/…

    – user7118434
    Dec 17 '16 at 13:27














70












70








70


19






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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












11 Answers
11






active

oldest

votes


















149














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();





share|improve this answer


























  • 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 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



















8














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();





share|improve this answer


























  • 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



















6














Use this global $post instead:



global $post;
echo $post->ID;





share|improve this answer


























  • 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



















4














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)






share|improve this answer

































    0














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






    share|improve this answer
























    • 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



















    0














    This function get id off a page current.



    get_the_ID();





    share|improve this answer



















    • 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



















    0














    Use below two lines of code to get current page or post ID



    global $post;
    echo $post->ID;





    share|improve this answer































      0














      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.






      share|improve this answer

































        0














        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];






        share|improve this answer































          -3














          This is the correct code.



          echo $post->ID;





          share|improve this answer

































            -3














            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!






            share|improve this answer





















            • 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














            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            149














            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();





            share|improve this answer


























            • 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 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
















            149














            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();





            share|improve this answer


























            • 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 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














            149












            149








            149







            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();





            share|improve this answer















            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();






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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 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



















            • 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 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

















            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













            8














            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();





            share|improve this answer


























            • 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
















            8














            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();





            share|improve this answer


























            • 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














            8












            8








            8







            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();





            share|improve this answer















            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();






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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 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



















            • 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

















            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











            6














            Use this global $post instead:



            global $post;
            echo $post->ID;





            share|improve this answer


























            • 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
















            6














            Use this global $post instead:



            global $post;
            echo $post->ID;





            share|improve this answer


























            • 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














            6












            6








            6







            Use this global $post instead:



            global $post;
            echo $post->ID;





            share|improve this answer















            Use this global $post instead:



            global $post;
            echo $post->ID;






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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



















            • 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











            4














            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)






            share|improve this answer






























              4














              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)






              share|improve this answer




























                4












                4








                4







                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)






                share|improve this answer















                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)







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 6 '14 at 15:26









                blade19899

                4171527




                4171527










                answered Jul 11 '13 at 21:52









                banestobanesto

                1,27811113




                1,27811113























                    0














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






                    share|improve this answer
























                    • 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
















                    0














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






                    share|improve this answer
























                    • 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














                    0












                    0








                    0







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






                    share|improve this answer













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







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    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



















                    • 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











                    0














                    This function get id off a page current.



                    get_the_ID();





                    share|improve this answer



















                    • 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
















                    0














                    This function get id off a page current.



                    get_the_ID();





                    share|improve this answer



















                    • 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














                    0












                    0








                    0







                    This function get id off a page current.



                    get_the_ID();





                    share|improve this answer













                    This function get id off a page current.



                    get_the_ID();






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    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














                    • 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











                    0














                    Use below two lines of code to get current page or post ID



                    global $post;
                    echo $post->ID;





                    share|improve this answer




























                      0














                      Use below two lines of code to get current page or post ID



                      global $post;
                      echo $post->ID;





                      share|improve this answer


























                        0












                        0








                        0







                        Use below two lines of code to get current page or post ID



                        global $post;
                        echo $post->ID;





                        share|improve this answer













                        Use below two lines of code to get current page or post ID



                        global $post;
                        echo $post->ID;






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jul 3 '17 at 14:08









                        Braj Kishor SahBraj Kishor Sah

                        6415




                        6415























                            0














                            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.






                            share|improve this answer






























                              0














                              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.






                              share|improve this answer




























                                0












                                0








                                0







                                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.






                                share|improve this answer















                                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.







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Oct 4 '17 at 17:35

























                                answered Oct 4 '17 at 17:16









                                Jarrett BarnettJarrett Barnett

                                62039




                                62039























                                    0














                                    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];






                                    share|improve this answer




























                                      0














                                      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];






                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        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];






                                        share|improve this answer













                                        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];







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Feb 26 '18 at 19:41









                                        EddEdd

                                        287




                                        287























                                            -3














                                            This is the correct code.



                                            echo $post->ID;





                                            share|improve this answer






























                                              -3














                                              This is the correct code.



                                              echo $post->ID;





                                              share|improve this answer




























                                                -3












                                                -3








                                                -3







                                                This is the correct code.



                                                echo $post->ID;





                                                share|improve this answer















                                                This is the correct code.



                                                echo $post->ID;






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Jun 14 '13 at 10:30









                                                Taryn

                                                193k47295358




                                                193k47295358










                                                answered Jun 14 '13 at 10:29









                                                BannaBanna

                                                11




                                                11























                                                    -3














                                                    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!






                                                    share|improve this answer





















                                                    • 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


















                                                    -3














                                                    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!






                                                    share|improve this answer





















                                                    • 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
















                                                    -3












                                                    -3








                                                    -3







                                                    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!






                                                    share|improve this answer















                                                    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!







                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    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
















                                                    • 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




















                                                    draft saved

                                                    draft discarded




















































                                                    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.




                                                    draft saved


                                                    draft discarded














                                                    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





















































                                                    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
                                                    CD,cOm Bh1W,oep,e p3By,Pyrj2vEHKB0 BnADFSpIDssPZzsYC7k2pBwX3RDhRk,69VSsabvFD8UXjdC tc9 9uiEC4YZ6u2tWH2E

                                                    Popular posts from this blog

                                                    mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

                                                    How to change which sound is reproduced for terminal bell?

                                                    Can I use Tabulator js library in my java Spring + Thymeleaf project?