WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How to get the current page id

[Resolved] How to get the current page id

  • snstar2006
    Member

    @snstar2006

    I want to use current page id in PHP to do a conditional branch in the template, but when I use $id. $post->ID, or the_ID(), I got the wrong ID.

    For example, the target page I want to make a difference is ID 7, but when I use the_ID() I got 1306 returned, which is the newest blog post that I just made.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Michael
    Moderator

    @alchymyth

    where exactly are you checking this?

    what else is going on on that page?

    any other loops or plugins or widgets?

    what if you add wp_reset_query(); before checking the page id?

    snstar2006
    Member

    @snstar2006

    wp_reset_query(); did not work for me. 🙁

    What I want to do is to create something like a index page, with a list of links to specific posts of my choice.

    So I want to use page id to tell the program that when it is the index page showing, do process A, else (when it in the post page) do process B.

    snstar2006
    Member

    @snstar2006

    page id still not working for me, so I use the last method that I want to use, using $_SERVER[“REQUEST_URI”] to get the URL to distinguish between the pages.

    Michael
    Moderator

    @alchymyth

    the index page does not have a page id;

    try to use the conditional tag is_home()

    http://codex.wordpress.org/Conditional_Tags

    snstar2006
    Member

    @snstar2006

    Great! it worked perfectly.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] How to get the current page id’ is closed to new replies.