首页 期权学习期权知识正文

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

xiaojiucai 期权知识 2020-08-18 477 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;

}

原文链接:https://www.qiquanji.com/post/8270.html

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

微信扫码关注

更新实时通知

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论