WordPress.org

Ready to get started?Download WordPress

Forums

Get current page ID (NOT post ID) (4 posts)

  1. MattParkins
    Member
    Posted 2 years ago #

    I'm in a page (NOT a post) and am trying to retrieve the Page ID using some embedded PHP in the page (using a PHP exec plugin). So far I've tried:

    <?php echo "pageid: ".$page->ID; ?>

    $page->ID returns empty, but using:
    $post->ID returns empty too,
    $get_the_ID() instead causes an error and stops execution,
    $the_ID() ditto above,
    $wp_query->post->ID returns empty, and finally:

    $this_page_id = get_query_var('page_id');
    echo "pageid: ".$this_page_id; ?>

    returns "pageid: 0"

    Any ideas what I could be doing wrong?

  2. MattParkins
    Member
    Posted 2 years ago #

    Solved. I hate wordpress.

    The solution was to use $post->ID as above, except IT NEEDS A global $post; first. Seriously, where is that in the docs?

    <?php echo "pageid: ".$post->ID; ?> // DOES NOT WORK

    <?php
    global $post;
    echo "pageid: ".$post->ID;
    ?>

    It is probably related to some php.ini setting, or PHP5 or something, but some kind of warning somewhere would have been nice.

  3. rickleijtencom
    Member
    Posted 2 years ago #

    There is no difference in page or post id.
    Everything is a post, pages just have a different post_type.

    <?php the_ID(); ?> (inside the loop!)

  4. MattParkins
    Member
    Posted 2 years ago #

    Thanks for that, Rick, that helps me grasp what is going on.

    Unfortunately the_ID() did not work for me presumably because I wasn't inside the loop, but in a single page. In fact the_ID() killed execution.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.