前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WordPress 技巧:获取 WordPress 后台当前页面的 Post type

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

作者头像
Denis
发布2023-04-15 10:10:32
3520
发布2023-04-15 10:10:32
举报
文章被收录于专栏:WordPress果酱WordPress果酱

我们在进行 WordPress 二次开发的时候,有时候需要知道 WordPress 后台当前页面的 Post type,下面的代码可以帮到我们:

代码语言:javascript
复制
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;
}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档