อะไรคือความแตกต่างระหว่าง $ post_id และ get_the_ID ()


คำตอบ:


17

get_the_ID()ส่งกลับID ของโพสต์ในปัจจุบันget_postโดยวิธีการ รับโพสต์ถือว่าglobal$postโพสต์ปัจจุบันเป็นตัวแปรเว้นแต่ว่าผ่านพารามิเตอร์ที่ระบุเป็นอย่างอื่น นั่นหมายความว่าควรกลับค่าเช่นเดียวกับที่มีอยู่ในget_the_ID() $post->IDการใช้get_the_ID()ช่วยให้คุณหลีกเลี่ยงการติดต่อกับโลกโดยตรงได้แม้ว่าคุณจะติดต่อกับทางอ้อม การใช้get_the_ID()มีแนวโน้มที่จะเกิดข้อผิดพลาดน้อยในไฟล์เท็มเพลตธีม

$post_idเป็นชื่อตัวแปรที่ใช้โดยทั่วไปเพื่ออ้างอิงรหัสโพสต์ แต่ไม่ใช่ตัวแปรหลักในแบบที่$postเป็นอยู่แม้ว่าจะได้รับการกำหนดสองสามครั้งในบริบทเฉพาะ:

grep -Rn '$ post_id' * | grep global
wp-admin / include / class-wp-comments-list-table.php: 25: global $ post_id;
wp-admin / include / class-wp-comments-list-table.php: 45: global $ post_id, $ comment_status, $ search, $ comment_type;
wp-admin / include / class-wp-comments-list-table.php: 141: global $ post_id, $ comment_status, $ comment_type;
wp-admin / include / class-wp-comments-list-table.php: 250: global $ post_id;
wp-admin / include / ajax-actions.php: 691: ทั่วโลก $ wp_list_table, $ post_id;
wp-include / ms-deprecated.php: 115: ฟังก์ชัน clear_global_post_cache ($ post_id) {
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.