×

WordPress 后台获取当前页面的 Post type类型

优畅设计 Wordpress技术 2019-06-25 14:41 3,096 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;
}

如果本文对你有帮助,你可以扫描右边的二维码打赏,谢谢支持
联系优畅:uctheme#qq.com (#改为@)
微信订阅号:优畅主题(uctheme)
官网淘宝店:http://uctheme.taobao.com
版权声明:版权归 优畅主题 所有,转载请注明出处!
转载请保留链接: https://www.uctheme.com/technical/2819.html
谢谢支付宝打赏
谢谢微信打赏

品牌创立:2012-11-18优畅主题

优畅主题成立于2012年11月18日,专业Wordpress导购主题开发商,拥有多年Wordpress主题设计经验,专门为淘宝客和导购客站长量身打造高端赚钱模板,我们注重细节,有着严谨的开发态度,一切从客户角度出发,如果你也喜欢 WordPress导购主题,欢迎和我们一起交流!


10 + 1 = ?

目前还没有评。

切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

切换登录

注册