How to get the specific Page’s current ID and use it in a get_page function?

Example of the code:

$page_id = 116; // 123 should be replaced with a specific Page's id from your site, which you can find by mousing over the link to edit that Page on the Manage Pages admin page. The id will be embedded in the query string of the URL, e.g. page.php?action=edit&post=123.
$page_data = get_page( $page_id ); // You must pass in a variable to the get_page function. If you pass in a value (e.g. get_page ( 123 ); ), WordPress will generate an error.

$content = apply_filters('the_content', $page_data->post_content); // Get Content and retain WordPress filters such as paragraph tags. Origin from: http://wordpress.org/support/topic/get_pagepost-and-no-paragraphs-problem
$title = $page_data->post_title; // Get title
echo $title; // Output Content
echo $content; // Output Content
?>

Instead of 166 (which was manually entered), I would like to retrieve the ID of the current page.

Read More

When i do $page_id = $post -> ID it retrieves the title and content of thte first post of the loop below (this is the posts page):

<div class="container">
                <?php // find all content that has the type of video and then to loop through them. ?>
                <?php query_posts('showpost'); ?>

                <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

                                <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                                    <?php if ( is_front_page() ) { ?>
                                        <h2 class="entry-title"><?php the_title(); ?></h2>
                                    <?php } else { ?>
                                        <h1 class="entry-title"><?php the_title(); ?></h1>
                                    <?php } ?>

                                    <div class="entry-content">
                                        <?php the_content(); ?>
                                        <?

php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
                                            <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
                                        </div><!-- .entry-content -->
                                    </div><!-- #post-## -->

                                    <?php comments_template( '', true ); ?>

                    <?php endwhile; ?>
        </div>

I would like to retrieve the code of the current page dynamically (which is 116).

What is the code to accomplish that?

Related posts

Leave a Reply

5 comments

  1. From quick test $wp_query->get_queried_object_id() should get page’s ID when that page is set to be posts page.

    This is likely the issue of timing it late enough that it is available, but early enough so that loop of posts doesn’t interfere. I’d try to capture it early (in template_redirect hook or around that) and store in global variable to use later.

  2. There’s two methods, depending on if you’re doing this inside or outside the loop.

    Inside:
    $page_id = $post->ID; (which you’ve mentioned, with no success, so I’ll assume you’re try for the alternative which is…)

    Outside:
    $page_id = $wp_query->post->ID;

  3. For those of you who this still isn’t working for, you will need to use some sort of add_action (you can choose which one you want to use). For my example, this will return the current page ID without any problems, regardless of whether in a plugin folder, functions php, or elsewhere.

    add_action('template_redirect', 'showid');
    
    function showid(){
        global $wp_query;
        $theid = intval($wp_query->queried_object->ID);
        echo $theid;
    }
    

    Good luck and happy coding!