檢索并且得到WordPress崗位ID在圈之外當PHP可變物

在WordPress,每個崗位有獨特的船錨標識符以數字崗位ID.的形式 當在圈裡面, WordPress處理并且顯示在當前時期將顯示的每一個崗位并且根據時怎樣格式化他們他們在圈標記之內配比指定了標準,模板標記the_ID可以用於容易地顯示崗位的ID在崗位裡面。 二者擇一地,在the_content過濾器裡面,宣稱$id全球性并且使用$id檢索崗位ID.

the_ID模板標記可以用於以下格式:

<?php the_ID (); ?>

在圈之外或在崗位的上下文外面,例如在步行者倒栽跳水和甚而側杆區域在WordPress的blog模板, the_ID ()不可能使用作為作用。 反而, $post->ID將用於退回崗位ID (句法可能也使用在崗位的圈裡面通過宣稱$post如全球性)。 $post是舉行關於在頁顯示的崗位的各種各樣的信息的一個全球性對象。 如此$post->ID將退回崗位的崗位ID。 $post使用在作用的它, $post裡面必須被宣稱作為一個全局變量。 例如:


//運作在圈裡面
作用function_name () {
全球性$post;
$thePostID = $post->ID;
}

或:


//在唯一崗位運作在圈外面
作用function_name () {
全球性$wp_query;
$thePostID = $wp_query->post->ID;
}

$post->ID可以叫太直接地在圈外面在一塊唯一崗位模板,例如 <?php回聲$post->ID ?> 將打印顯示崗位的ID數字。

在多個崗位觀看頁例如索引頁或檔案頁,檢索得到新是可能的或最早期的崗位所有崗位ID在頁顯示了通過使用命令由條目命令崗位不遲於日期,然後設置紀錄數限制1。

例如和有各種各樣的方式使用詢問:

//Get最新的崗位ID數字
$post->ID命令由post_date ASC極限1

//Get最早的崗位ID數字
$post->ID命令由post_date DESC極限1

重要: 這是現狀提供,不用保單的機器翻譯的頁。 計算機翻譯也許是難瞭解。 參見 原始的英國文章 每當可能。

分享并且貢獻或者得到技術支持并且幫助在 我的數字式生活論壇.



13 Responses to “Retrieve and Get WordPress Post ID Outside the Loop as PHP Variable”

  1. Joel P
    December 1st, 2006 06:06
    1

    This is really good thing to know… I think i’ll try to use this with my blog and limited php knowledge.

  2. Matt
    May 3rd, 2007 05:10
    2

    Just what I was looking for, thanks!

  3. Christophe
    May 16th, 2007 14:15
    3

    Found this through Google. Thanks! You should also enter this into the official WP documentation wiki: http://codex.wordpress.org/Main_Page

  4. k
    June 28th, 2007 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
    5

    Thanks, using ‘global $id;’ in a filter for my plugin got me the post id I needed.

  6. Osordvibil
    October 6th, 2007 00:09
    6

    comment_fashion

  7. 让WP不同页面使用不同设计 | 收拾起大地山河一担装
    October 10th, 2007 21:05
    7

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

  8. Reviews
    April 18th, 2008 18:11
    8

    Thanks! That’s just what i am looking for. I want to get post id on the wordpress index (root) page.

  9. Shops
    May 6th, 2008 20:02
    9

    Thanks for this. You cant believe how long it took me to search for this simple answer! I dont know why you cant find it on wordpress.

  10. Johannes
    June 16th, 2008 03: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.
    Now I want to implement a feature like shadowbox to popup the images. To do this automatically, I put a rel=”shadowbox[imageset]” in the anchors of the images.
    What do I have to do to put the post-title or ID instead of imageset.
    Is something like this possible?

  11. Johannes
    June 16th, 2008 03:27
    11

    Forgot Notification of followups. Thanks.

  12. PieterC
    June 17th, 2008 14:02
    12

    Thank you. It works like a charm!

  13. Daniel
    June 18th, 2008 09:34
    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>

Subscribe without commenting


Custom Search

New Articles

Incoming Search Terms for the Article

wordpress post id - wordpress get post id - wordpress get page id - post id wordpress - wordpress page id - get post ID Wordpress - $post->ID - wordpress $post->id - wordpress the_ID - wordpress get id - wordpress get current page id - wordpress post object - wordpress current page id - wordpress get current page - page ID wordpress - wordpress post_id - $post->ID wordpress - wordpress get category id - wordpress if post id - wordpress get post by id - wordpress current post id - wordpress post variable - wordpress get post - get post id - wordpress post->ID - wordpress $post object - wordpress postid - wordpress $post - post id - wordpress get current post - get id wordpress - wordpress post variables - get post id in wordpress - wordpress pageid - wordpress $post variable - wordpress get current category id - get page ID wordpress - wordpress get id of current page - wordpress get current post id - wordpress get post category id - post->id - wordpress $post_id - wordpress page id outside loop - get wordpress post id - wordpress the_id() - wordpress get page by id - wordpress id post - wordpress change post id - wordpress get posts - wordpress get title by id -