• 欢迎访问IT乐园(o゚▽゚)o
  • 推荐使用最新版火狐浏览器和Chrome浏览器访问本网站。

WordPress 技巧:获取WordPress后台当前页面的Post type

php fhy 7年前 (2017-10-30) 4644次浏览 0个评论

我们在进行 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;
}

 


IT 乐园 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:WordPress 技巧:获取 WordPress 后台当前页面的 Post type
喜欢 (3)
关于作者:
九零后挨踢男
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址