百度 | 神马 | 搜狗 | 技术文档 | 学习资料分享 - 记录帝国CMS及运维技术的点点滴滴
你的位置:首页 > 实用IT技术 » 正文

【Wordpress相关】WordPress 技巧:获取 WordPress 后台当前页面的 Post type

发布时间:2020-09-17

作品分类:Wordpress相关  后台  主机  页面  后台  获取  页面  技巧  WordPress

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

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

本站推荐使用的主机:,国外主机建议使用

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>

Top