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.
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?
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.but if you’re using a custom page for posts, where are you adding this code?
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;
I replaced:
with:
Worked for me!
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.
Good luck and happy coding!