Forums

[resolved] How to return current page's id? (13 posts)

  1. brian914
    Member
    Posted 1 year ago #

    I would like to return the current page's id(the page I am on), so I can compare that with a list of links I have and remove linking if I am already on the page for that link.

    I tried a number of things, but can't seem to get the id of the page I am on.

    Thanks a lot for any help!

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Try using $post->ID.

  3. brian914
    Member
    Posted 1 year ago #

    Yes, thank you so much!!!

  4. brian914
    Member
    Posted 1 year ago #

    Wait, no, I got too excited :-)

    If I do that, I get the same for both of the following:

    echo '<li>Page Number: '; echo $post->ID; echo '</li>';
    echo '<li>Post Number: '; the_ID(); echo '</li>';

    What I want is the id of the page that this loop runs on. Here is more of the code, maybe this makes it more clear.

    Thank you so much for the help.

    [Code moderated as per the Forum Rules. Please use the pastebin]

  5. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Not all web pages have ids. Archive (post listing pages) such as your home/blog page, the tag pages and the category pages, for example. They are not "pages" in the accepted sense of the word.

  6. brian914
    Member
    Posted 1 year ago #

    I know that these have ids, because I can see them via this:
    echo '<li>Post Number: '; the_ID(); echo '</li>';

    I just don't know how to get the current one, when I am on one of those pages.

  7. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    But are these WordPress Pages?

  8. brian914
    Member
    Posted 1 year ago #

    Yes, they are pages. I can actually go in my admin panel and they show a number.

  9. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Are you grabbing the page id before your custom query & Loop?

  10. brian914
    Member
    Posted 1 year ago #

    No inside. Should I create a variable and add it before running the loop? How would I do that?

    I have the following right now for the loop:

    [Code moderated as per the Forum Rules. Please use the pastebin]

  11. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Should I create a variable and add it before running the loop?

    Yes. Try something like:

    <?php $this_page = $post->ID;?>

    before setting up any custom query.

  12. brian914
    Member
    Posted 1 year ago #

    Yes, that did it!!! Thank you so much!!!

  13. brian914
    Member
    Posted 1 year ago #

    I guess we came full circle. Sorry, I am new to wp and didn't realize that it would have to be before the loop.

Topic Closed

This topic has been closed to new replies.

About this Topic