$ post_id和get_the_ID()有什么区别?


Answers:


17

get_the_ID()返回当前帖子的ID的方式get_post。获取帖子假定当前帖子为global变量,$post除非传递的参数另外指定。这意味着get_the_ID()应该返回与中包含的相同值$post->ID。使用get_the_ID()可以避免直接处理全局,尽管您要间接处理它。使用get_the_ID()通常在主题模板文件中不太容易出错。

$post_id是一个变量名,通常用于引用帖子ID,但它并不是Core变量$post,尽管它确实在特定上下文中定义了几次:

grep -Rn'$ post_id'* | grep全局
wp-admin / includes / class-wp-comments-list-table.php:25:全局$ post_id;
wp-admin / includes / class-wp-comments-list-table.php:45:全局$ post_id,$ comment_status,$ search,$ comment_type;
wp-admin / includes / class-wp-comments-list-table.php:141:全局$ post_id,$ comment_status,$ comment_type;
wp-admin / includes / class-wp-comments-list-table.php:250:全局$ post_id;
wp-admin / includes / ajax-actions.php:691:全局$ wp_list_table,$ post_id;
wp-includes / ms-preprecated.php:115:function clear_global_post_cache($ post_id){
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.