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 我的数字生活论坛 .



13 Responses to “Retrieve and Get WordPress Post ID Outside the Loop as PHP Variable” 13日的反应“检索和获得的WordPress张贴ID以外的回路作为PHP变量”

  1. Joel P约珥p
    December 1st, 2006 06:06 2006年12月1日6时06分
    1

    This is really good thing to know… I think i’ll try to use this with my blog and limited php knowledge.这实在是件好事,知道吗…我想我会尝试使用此与我的博客和有限的PHP的知识。

  2. Matt马特
    May 3rd, 2007 05:10 2007年5月3日5时10分
    2

    Just what I was looking for, thanks!究竟是什么我一直在寻找,谢谢!

  3. Christophe克里斯多夫
    May 16th, 2007 14:15 2007年5月16日14时15分
    3

    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

  4. k
    June 28th, 2007 11:55 2007年6月28日11时55分
    4

    Great info.伟大的信息。 Needed this a few days ago.这需要数天前。 Will update my code with this direct approach.将更新我的代码与此直接的方法。 Thanks.谢谢。

  5. Eric李家祥
    September 21st, 2007 07:51 2007年9月21日7时51分
    5

    Thanks, using ‘global $id;’ in a filter for my plugin got me the post id I needed.感谢,使用'全球编号美元; '在一个过滤器为我的插件让我邮政编号我需要。

  6. Osordvibil osordvibil
    October 6th, 2007 00:09 2007年10月6日0时09分
    6

    comment_fashion

  7. 让WP不同页面使用不同设计 | 收拾起大地山河一担装让可湿性粉剂不同页面使用不同设计|收拾起大地山河一担装
    October 10th, 2007 21:05 2007年10月10日21时05分
    7

    [...] 使用post ID变量可以做到。$post->ID可以返回文章的唯一编号。 [...] [ … … ]使用邮政编号变量可以做到。元后“ ->编号可以返回文章的唯一编号。 [ … … ]

  8. Reviews评语
    April 18th, 2008 18:11 2008年4月18日18时11分
    8

    Thanks!谢谢! That’s just what i am looking for.这只是什么,我期待。 I want to get post id on the wordpress index (root) page.我想获得张贴ID就在WordPress指数(根)网页。

  9. Shops商店
    May 6th, 2008 20:02 2008年5月6日20时02分
    9

    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 。

  10. Johannes约翰内斯
    June 16th, 2008 03:27 2008年6月16日3时27分
    10

    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?是这样的可能吗?

  11. Johannes约翰内斯
    June 16th, 2008 03:27 2008年6月16日3时27分
    11 11月

    Forgot Notification of followups.忘了通知followups 。 Thanks.谢谢。

  12. PieterC pieterc
    June 17th, 2008 14:02 2008年6月17日14时02分
    12 12月

    Thank you.谢谢您。 It works like a charm!它像一个魅力!

  13. Daniel丹尼尔
    June 18th, 2008 09:34 2008年6月18日09:34
    13 13日

    Thanks for this - this did absolutely what I needed to do.感谢这一点-这绝对没有什么我需要做的。 : )

Leave a Reply离开的答复

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> 您可以使用这些标签:的<a href="" title=""> <abbr title=""> <acronym title="">的<b> <blockquote cite=""> <cite>的<code> <删除日期时间= “ ” >的<em>的<i> <q cite=""> <strike>的<strong>

Subscribe without commenting订阅无评论


Custom Search

New Articles新的条款,

Incoming Search Terms for the Article传入的搜索条件文章

wordpress post id WordPress所张贴ID - - wordpress get post id WordPress所取得张贴ID - - wordpress get page id WordPress所获得页ID - - post id wordpress 邮政编号的WordPress - - wordpress page id 在WordPress页ID - - get post ID Wordpress 获得张贴ID的WordPress - - $post->ID 元后“ ->编号 - - wordpress $post->id 在WordPress元后“ ->编号 - - wordpress the_ID 在WordPress the_id - - wordpress get current page id WordPress所得到当前页ID - - wordpress post object 在WordPress邮政对象 - - wordpress get id WordPress所得到的ID - - wordpress get current page WordPress所得到当前页 - - wordpress current page id 在WordPress当前页ID - - page ID wordpress 页ID的WordPress - - wordpress get category id WordPress所获得类别ID - - wordpress if post id 在WordPress如果张贴ID - - wordpress post_id 在WordPress post_id - - $post->ID wordpress 元后“ ->编号的W ordPress - - wordpress get post by id WordPress所获得邮政编号 - - wordpress get post WordPress所获得邮政 - - wordpress current post id 在WordPress目前邮政编号 - - wordpress post variable 在WordPress后变 - - wordpress $post object 在WordPress元,邮政对象 - - wordpress post->ID 在WordPress后“ ->编号 - - wordpress postid 在WordPress postid - - wordpress $post 在WordPress元,邮政 - - get post id 获得张贴ID - - get id wordpress 获得编号的WordPress - - wordpress post variables 在WordPress后的变数 - - wordpress get current post WordPress所取得目前的员额 - - get post id in wordpress 获得张贴ID在WordPress - - post id 邮政编号 - - wordpress pageid 在WordPress pageid - - wordpress $post variable 在WordPress元后变 - - wordpress get current category id WordPress所取得目前的类别ID - - get page ID wordpress 获得网页ID的WordPress - - wordpress get current post id WordPress所取得目前的邮政编号 - - wordpress get post category id WordPress所获得邮政类别ID - - post->id 邮政“ ->编号 - - wordpress $post_id 在WordPress元post_id - - get wordpress post id 获得WordPress所张贴ID - - wordpress the_id() 在WordPress the_id ( ) - - wordpress get id of current page WordPress所得到的ID当前页 - - wordpress get page by id WordPress所获得网页编号 - - wordpress id post 在WordPress编号后 - - wordpress change post id 在WordPress改变邮政编号 - - wordpress get posts WordPress所获得的职位 - - the_ID wordpress the_id的WordPress - - wordpress outside the loop 在WordPress以外的闭环 - -