WordPress获取后台当前页面Post type的方法

  • A+
所属分类:WordPress教程

WordPress学习网(www.wordpressxxw.com)在进行 WordPress 二次开发的时候,有时候需要知道 WordPress 后台当前页面的 Post type,可采用下面代码实现:

 function get_current_post_type() {   global $post, $typenow, $current_screen;   //we have a post so we can just get the post type from that   if ( $post && $post->post_type ) {     return $post->post_type;   }   //check the global $typenow - set in admin.php   elseif ( $typenow ) {     return $typenow;   }   //check the global $current_screen object - set in sceen.php   elseif ( $current_screen && $current_screen->post_type ) {     return $current_screen->post_type;   }   //check the post_type querystring   elseif ( isset( $_REQUEST['post_type'] ) ) {     return sanitize_key( $_REQUEST['post_type'] );   }   //lastly check if post ID is in query string   elseif ( isset( $_REQUEST['post'] ) ) {     return get_post_type( $_REQUEST['post'] );   }   //we do not know the post type!   return null; } 

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: