admin管理员组文章数量:1289890
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:
$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?
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/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?
Share Improve this question edited Jan 26, 2011 at 10:53 wyc asked Jan 26, 2011 at 10:12 wycwyc 3,90719 gold badges61 silver badges97 bronze badges5 Answers
Reset to default 4From 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.
global $wp_query;
$page_id = $wp_query->get_queried_object_id();
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:
$page_id = [id of post];
with:
$page_id = $wp_query->get_queried_object_id();
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.
add_action('template_redirect', 'showid');
function showid(){
global $wp_query;
$theid = intval($wp_query->queried_object->ID);
echo $theid;
}
Good luck and happy coding!
本文标签: How to get the specific Page39s current ID and use it in a getpage function
版权声明:本文标题:How to get the specific Page's current ID and use it in a get_page function? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741402089a2376725.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论