WordPress.org

Forums

[resolved] How to get the current page id (6 posts)

  1. snstar2006
    Member
    Posted 2 years ago #

    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.

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    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?

  3. snstar2006
    Member
    Posted 2 years ago #

    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.

  4. snstar2006
    Member
    Posted 2 years ago #

    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.

  5. alchymyth
    Forum Moderator
    Posted 2 years ago #

    the index page does not have a page id;

    try to use the conditional tag is_home()

    http://codex.wordpress.org/Conditional_Tags

  6. snstar2006
    Member
    Posted 2 years ago #

    Great! it worked perfectly.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags