Retrieve and Get WordPress Post ID Outside the Loop as PHP Variable Lấy và Nhận WordPress Post ID Bên ngoài Loop như PHP Variable
In WordPress, each post has unique anchor identifier in the form of numeric post ID. Trong WordPress, đăng từng có định danh duy nhất neo dưới hình thức đăng số 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. Khi bên trong Vòng, nơi WordPress quy trình và hiển thị mỗi bài viết sẽ được hiển thị trên trang hiện tại và định dạng chúng theo các tiêu chí như thế nào họ phù hợp với quy định trong Các thẻ Vòng, mẫu thẻ the_ID có thể được sử dụng để dễ dàng hiển thị ID của bài bên trong post. Alternatively, inside the the_content filter, declare $id global and use $id to retrieve the post ID. Ngoài ra, bên trong các bộ lọc the_content, tuyên bố $ id toàn cầu và sử dụng $ id để lấy đăng bài ID.
the_ID template tag can be used in the following format: the_ID mẫu thẻ có thể được sử dụng trong các định dạng sau:
<?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. Vòng ngoài hoặc ra khỏi bối cảnh của bài viết, chẳng hạn như trong tiêu đề của chân và thậm chí cả khu vực bên trong các mẫu của blog's, the_ID () không thể được sử dụng như là một hàm. 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). Thay vào đó, đăng $-> ID sẽ được dùng để trả lại bài ID (cú pháp cũng có thể được sử dụng bên trong Loop của post by $ post tuyên bố như trên toàn cầu). $post is a global object that holds various information about the posts displayed on the page. $ post là một đối tượng trên toàn cầu chứa các thông tin khác nhau về các bài viết được hiển thị trên trang. So $post->ID will return the post ID of the post. Vì vậy, $ post-> ID sẽ trả lại bài ID của bài này. It the $post is used inside a function, the $post has to be declared as a global variable. Đăng It của $ được sử dụng trong một hàm, các bài $ đã được khai báo là một biến toàn cầu. For example: Ví dụ:
// Works inside of the Loop
function function_name() {
global $post;
$thePostID = $post->ID;
}
or: hoặc:
// Works in single post outside of the Loop
function function_name() {
global $wp_query;
$thePostID = $wp_query->post->ID;
}
$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. $ post-> ID có thể được gọi trực tiếp bên ngoài Loop quá trong một bài đăng đơn mẫu, chẳng hạn như <? php echo $ post-> ID> sẽ in hiển thị số ID của bài?.
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. Trong bài viết nhiều lần xem trang như là trang index hay trang lưu trữ, có thể để lấy được bài mới nhất hoặc sớm nhất ID của tất cả các bài viết hiển thị trên trang bằng cách sử dụng một mệnh đề ORDER BY để đặt hàng các bài viết theo ngày, và sau đó đặt số lượng hồ sơ để LIMIT 1.
Just for example, and there are various way to use the query: Chỉ cần ví dụ, và có nhiều cách để sử dụng các truy vấn:
//Get the latest post ID number / / Get the latest số ID
$post->ID ORDER BY post_date ASC LIMIT 1 $ post-> ID post_date ORDER BY ASC LIMIT 1
//Get the earliest post ID number / / Get the post sớm nhất số ID
$post->ID ORDER BY post_date DESC LIMIT 1 $ post-> ID ORDER BY post_date DESC LIMIT 1
IMPORTANT : The page is machine translated and provided "as is" without warranty. Quan trọng: Sửa là máy dịch và cung cấp "như là" không có bảo hành. Machine translation may be difficult to understand. Máy dịch thuật có thể khó hiểu. Please refer to Vui lòng tham khảo original English article bản gốc tiếng Anh bài viết whenever possible. bất cứ khi nào có thể.
Related Articles Bài viết liên quan
- Paginating or Split WordPress Post or Page with NextPage in WordPress Not Working Paginating hoặc Split WordPress Đăng hoặc Trang với NextPage trong WordPress Không phải làm việc
- Disable and Turn Off Post Revisions Tracking in WordPress 2.6 or Above Vô hiệu hoá và Turn Off Đăng Revisions Theo dõi trong WordPress 2,6 hoặc Trên
- How to Delete Existing WordPress Post Revisions Stored/Saved Làm thế nào để Xóa hiện WordPress Đăng Revisions Lưu trữ / Saved
- How To Store Every Post Revisions on Each Auto-Save in WordPress with Limit on Versions Count Làm thế nào để Store Mỗi bản xem lại bài Mỗi ngày Auto-Save trong WordPress với giới hạn về Phiên Bản Count
- Adding Extra More Fields to WordPress Write/Edit Post/Page Right Column Thêm bổ sung thêm Fields để WordPress Viết / Sửa Post / Trang Ngay Cột
- How to Change the Frequency or Interval WordPress Auto Saves An Editing Post or Page Làm thế nào để thay đổi các tần số hoặc Interval WordPress Auto Tiết kiệm An Post Chỉnh sửa hoặc Trang
- How to Wrap AdSense Ads Around and Inline Beside Blogger Post Content Làm thế nào để quảng cáo AdSense của Wrap Around và Inline Ngoài Blogger Post Nội dung
- Move Blogger Inline Ads Between Posts to Inside Within Post Position Blogger di chuyển nội tuyến Quảng cáo Từ bài viết để Bên Trong Đăng Chức
- Place AdSense Ad Unit In Various Positions Inside Within Blogger Post Contents Đặt quảng cáo AdSense của đơn vị Trong Vị trí khác nhau Bên Trong Blogger Đăng lục
- How to Display Elements (Ads) Only On (Or Only Not At) Individual Single Post Item View Làm thế nào để hiển thị Elements (Quảng cáo) Chỉ On (Hoặc Chỉ Không Tại) Cá nhân Single Post Mã Xem










































November 7th, 2009 03:41 07 Tháng 11 2009 03:41
Hi, Chào,
I'm trying to modify a custom post-new.php in the admin area and need to access the post_id of a post that I'm currently editing and that has not yet been published. Tôi đang cố gắng để sửa đổi một bài tuỳ-new.php trong lĩnh vực quản trị và cần để truy cập post_id của đăng rằng tôi đang chỉnh sửa và chưa được xuất bản. Any idea how I could do that? Bất cứ ý tưởng làm thế nào tôi có thể làm điều đó?
Thanks Cảm ơn
October 30th, 2009 02:22 Ngày 30 tháng 10 năm 2009 02:22
Thanks, this was super-helpful and easy to understand! Thanks, đây là siêu hữu ích và dễ hiểu! I love code samples! Tôi thích mẫu mã!