Retrieve and Get WordPress Post ID Outside the Loop as PHP Variable检索和获取的WordPress张贴ID以外的回路作为PHP变量
In WordPress, each post has unique anchor identifier in the form of numeric post ID.在WordPress ,每一个职位有独特的锚标识符,在数字的形式张贴ID 。 When inside the Loop, where WordPress processes and displays each of the posts to be displayed on the current page and formats them according to how they match specified criteria within The Loop tags, template tag the_ID can be used to easily display the post’s ID inside the post.当内环路,那里的WordPress过程,并显示每个员额,以显示对当前页和格式,他们根据他们如何符合特定标准的内部循环标签,模板标记the_id可以用来很容易显示,邮政的ID内员额。 Alternatively, inside the the_content filter, declare $id global and use $id to retrieve the post ID.另外,内the_content过滤器,申报编号美元的全球和使用美元,编号,以撷取张贴ID 。
the_ID template tag can be used in the following format: the_id模板标记可以用在以下格式:
<?php the_ID(); ?> < ? PHP的the_id ( ) ; ? >
Outside the Loop or out of the context of the post, such as in header of footer and even sidebar area in the templates of WordPress’s blog, the_ID() cannot be used as a function.外回路或转出的背景后,如在标题的注脚,甚至栏地区,在模板的WordPress的博客, the_id ( )不能被用来作为一个功能。 Instead, $post->ID will be used to return the post ID (the syntax can also be used inside the Loop of the post by declaring $post as global).相反,美元后“ ->编号将被用来归还张贴I D(语法也可用于内回路后,宣布元后,作为全球) 。 $post is a global object that holds various information about the posts displayed on the page.元,邮政是一个全球性对象,举办各种有关职位的页面上显示。 So $post->ID will return the post ID of the post.因此,美元后“ -> I D将会返回后的I D后。 It the $post is used inside a function, the $post has to be declared as a global variable.它美元后是用来内发挥作用,美元后已被宣布为作为一个全球性的变数。 For example:例如:
// Works inside of the Loop / /工程,内环路
function function_name() { function_name的函数( ) (
global $post; 全球邮政元;
$thePostID = $post->ID; 元thepostid =元后“ ->身份证;
} )
or:或:
// Works in single post outside of the Loop / /工程,在单一的工作地点以外的河套
function function_name() { function_name的函数( ) (
global $wp_query; 全球wp_query元;
$thePostID = $wp_query->post->ID; 元thepostid = $ wp_query -> “后” - >身份证;
} )
$post->ID can be called directly outside of the Loop too in a single post template, such as <?php echo $post->ID ?> will print display the post’s ID number.元后“ ->编号,可以直接调用以外的循环也有一个单一的帖子模板,如< ? P HP的回波元后” - >I D吗? > “打印显示帖子的I D号码。
In the multiple posts view page such as index page or archive page, it’s possible to retrieve the get the latest or earliest post ID of all the posts displayed on the page by using an ORDER BY clause to order the posts by date, and then set the number of record to LIMIT 1.在多重的职位检视网页,如索引页或存档页,它的可能,以撷取获取最新的或最早的邮政ID的所有职位,显示在网页上使用ORDER BY子句的命令职位按日期,和然后设置数目的纪录,以限制1 。
Just for example, and there are various way to use the query:刚才举例来说,有很多不同的方法,可使用查询:
//Get the latest post ID number / /获取最新的邮政身份证号码
$post->ID ORDER BY post_date ASC LIMIT 1元后“ ->编号,以便由p ost_date的A SC限制1
//Get the earliest post ID number / /得到最早的邮政身份证号码
$post->ID ORDER BY post_date DESC LIMIT 1元后“ ->编号,以便由p ost_date递减的限制1
IMPORTANT : This is a machine translated page which is provided "as is" without warranty. 重要说明 :这是一个机器翻译网页是“按原样”提供的担保。 Machine translation may be difficult to understand.机器翻译可能很难理解。 Please refer to请参阅 original English article英文原版的文章 whenever possible.只要有可能。
Share and contribute or get technical support and help at分享和贡献,或取得技术的支持和帮助,在 My Digital Life Forums 我的数字生活论坛 . 。
Related Articles相关文章
- Paginating or Split WordPress Post or Page with NextPage in WordPress Not Working paginating或分裂的WordPress邮寄或网页下一页在WordPress没有工作
- Disable and Turn Off Post Revisions Tracking in WordPress 2.6 or Above禁用和关闭后的修改,跟踪在WordPress 2.6或以上
- How to Change the Frequency or Interval WordPress Auto Saves An Editing Post or Page如何改变频率或间隔的WordPress自动保存编辑的职位或网页
- WordPress MySQL SQL Query Error in WPDB Class在WordPress MySQL的SQL查询错误在wpdb级
- Adding Extra More Fields to WordPress Write/Edit Post/Page Right Column加入额外的更多领域的WordPress写/编辑帖子/页右边一栏
- Disable Auto Go To (Jump) To Read More Tag禁用自动转到(跳转)来读取更多的标签
- How To Store Every Post Revisions on Each Auto-Save in WordPress with Limit on Versions Count如何存储每一个岗位的修改对每个自动储存在WordPress与限制的版本计数
- 3 Column Relaxation WordPress Theme comments-paged.php for Paged-Comments WordPress Plugin三栏放宽的WordPress主题的评论- paged.php为分页-评论wordpress插件
- How to Delete Existing WordPress Post Revisions Stored/Saved如何删除现有的WordPress修改后的储存/保存
- Integrate WordPress including Comments with bbPress Forum using bbSync整合WordPress所包括的评论与bbpress论坛使用bbsync



















December 1st, 2006 06:06 2006年12月1日6时06分
This is really good thing to know… I think i’ll try to use this with my blog and limited php knowledge.这实在是件好事,知道吗…我想我会尝试使用此与我的博客和有限的PHP的知识。
May 3rd, 2007 05:10 2007年5月3日5时10分
Just what I was looking for, thanks!究竟是什么我一直在寻找,谢谢!
May 16th, 2007 14:15 2007年5月16日14时15分
Found this through Google.发现这透过Google 。 Thanks!谢谢! You should also enter this into the official WP documentation wiki:你也应该进入到这一点官方可湿性粉剂文件的wiki : http://codex.wordpress.org/Main_Page http://codex.wordpress.org/main_page
June 28th, 2007 11:55 2007年6月28日11时55分
Great info.伟大的信息。 Needed this a few days ago.这需要数天前。 Will update my code with this direct approach.将更新我的代码与此直接的方法。 Thanks.谢谢。
September 21st, 2007 07:51 2007年9月21日7时51分
Thanks, using ‘global $id;’ in a filter for my plugin got me the post id I needed.感谢,使用'全球编号美元; '在一个过滤器为我的插件让我邮政编号我需要。
October 6th, 2007 00:09 2007年10月6日0时09分
comment_fashion
October 10th, 2007 21:05 2007年10月10日21时05分
[...] 使用post ID变量可以做到。$post->ID可以返回文章的唯一编号。 [...] [ … … ]使用邮政编号变量可以做到。元后“ ->编号可以返回文章的唯一编号。 [ … … ]
April 18th, 2008 18:11 2008年4月18日18时11分
Thanks!谢谢! That’s just what i am looking for.这只是什么,我期待。 I want to get post id on the wordpress index (root) page.我想获得张贴ID就在WordPress指数(根)网页。
May 6th, 2008 20:02 2008年5月6日20时02分
Thanks for this.感谢。 You cant believe how long it took me to search for this simple answer!你cannot相信多久,我花了搜索,这答案很简单! I dont know why you cant find it on wordpress. i不知道你为什么cannot中找到它的WordPress 。
June 16th, 2008 03:27 2008年6月16日3时27分
Hi,嗨,
thanks for these informations.感谢这些信息。
I tried to configure something without any luck and hope you can help me.我试图配置的东西没有什么好运,并希望您能帮助我。
I’m using wordpress as a blog and do have a few pictures in every blog.我使用的WordPress作为博客和做有一些图片在每一个博客。
Now I want to implement a feature like shadowbox to popup the images.现在我要执行的功能一样, shadowbox到弹出的图像。 To do this automatically, I put a rel=”shadowbox[imageset]” in the anchors of the images.为此,自动,我提出的rel = “ shadowbox [ imageset ] : ”在主播的形象。
What do I have to do to put the post-title or ID instead of imageset.我该怎么做必须做的,把后的名称或ID而不是imageset 。
Is something like this possible?是这样的可能吗?
June 16th, 2008 03:27 2008年6月16日3时27分
Forgot Notification of followups.忘了通知followups 。 Thanks.谢谢。
June 17th, 2008 14:02 2008年6月17日14时02分
Thank you.谢谢您。 It works like a charm!它像一个魅力!
June 18th, 2008 09:34 2008年6月18日09:34
Thanks for this - this did absolutely what I needed to do.感谢这一点-这绝对没有什么我需要做的。