如何确定我们是在WordPress管理中的添加新页面/POST/CPT中还是在编辑页面/POST/CPT中?

时间:2012-04-24 作者:Dipesh KC

这看起来很简单,但我需要一种方法来确定当前屏幕是否Add NewEdit (一种wordpress管理条件标记)。是否已经有用于此的内置函数,或者。。。你知道如何实现吗?

4 个回复
最合适的回答,由SO网友:Bainternet 整理而成

我有一个函数:

/**
 * is_edit_page 
 * function to check if the current page is a post edit page
 * 
 * @author Ohad Raz <[email protected]>
 * 
 * @param  string  $new_edit what page to check for accepts new - new post page ,edit - edit post page, null for either
 * @return boolean
 */
function is_edit_page($new_edit = null){
    global $pagenow;
    //make sure we are on the backend
    if (!is_admin()) return false;

    
    if($new_edit == "edit")
        return in_array( $pagenow, array( \'post.php\',  ) );
    elseif($new_edit == "new") //check for new post page
        return in_array( $pagenow, array( \'post-new.php\' ) );
    else //check for either new or edit
        return in_array( $pagenow, array( \'post.php\', \'post-new.php\' ) );
}
Usage与任何其他条件标记一样,用法很简单,很少有示例:

检查新页面或编辑页面:

if (is_edit_page()){
   //yes its an edit/new post page
}
检查新帖子页面:

if (is_edit_page(\'new\')){
   //yes its an new post page
}
检查编辑帖子页面:

if (is_edit_page(\'edit\')){
   //yes its an existing post page
}
将此与$typenow 要检查特定帖子类型编辑页面,请执行全局操作:

global $typenow;
if (is_edit_page(\'edit\') && "Post_Type_Name" == $typenow){
   //yes its an edit page  of a custom post type named Post_Type_Name
}

SO网友:That Brazilian Guy

我更喜欢get_current_screen(), 因为这要简单得多:

$screen = get_current_screen();
  if ($screen->base == "post") {
}
此函数返回一个对象,该对象包括屏幕的ID、基、帖子类型和分类以及其他数据点。

(Codex)

SO网友:Aamer Shahzad

我想enqueue scriptstyles 仅在特定new/edit 立柱式屏幕。创建了一个函数来检查我是否处于打开状态edit/new-post 给定的屏幕CPT.

/**
 * Check if \'edit\' or \'new-post\' screen of a 
 * given post type is opened
 * 
 * @param null $post_type name of post type to compare
 *
 * @return bool true or false
 */
function is_edit_or_new_cpt( $post_type = null ) {
    global $pagenow;

    /**
     * return false if not on admin page or
     * post type to compare is null
     */
    if ( ! is_admin() || $post_type === null ) {
        return FALSE;
    }

    /**
     * if edit screen of a post type is active
     */
    if ( $pagenow === \'post.php\' ) {
        // get post id, in case of view all cpt post id will be -1
        $post_id = isset( $_GET[ \'post\' ] ) ? $_GET[ \'post\' ] : - 1;

        // if no post id then return false
        if ( $post_id === - 1 ) {
            return FALSE;
        }

        // get post type from post id
        $get_post_type = get_post_type( $post_id );

        // if post type is compared return true else false
        if ( $post_type === $get_post_type ) {
            return TRUE;
        } else {
            return FALSE;
        }
    } elseif ( $pagenow === \'post-new.php\' ) { // is new-post screen of a post type is active
        // get post type from $_GET array
        $get_post_type = isset( $_GET[ \'post_type\' ] ) ? $_GET[ \'post_type\' ] : \'\';
        // if post type matches return true else false.
        if ( $post_type === $get_post_type ) {
            return TRUE;
        } else {
            return FALSE;
        }
    } else {
        // return false if on any other page.
        return FALSE;
    }
}
要使用该函数,请传递post类型名称。

/**
 * return true if \'edit\' or \'new-post\' screen of \'cpt_name\' is opened
 */
if ( is_edit_or_new_cpt(\'cpt_name\') ) {
    // do something
}

SO网友:Ahmad Wael

我有这个功能

if(!function_exists(\'is_post_edit_page\')){
    function is_post_edit_page() {
        static $result = null;
        if ( $result === null ) {
            $result = false;
            if ( is_admin() ) {
                if (
                    empty( $_POST )
                    &&
                    isset( $_GET[\'action\'] )
                    &&
                    $_GET[\'action\'] === \'edit\'
                    &&
                    isset( $_GET[\'post\'] )
                ) {
                    // Display Edit Post page
                    $result = true;
                } elseif (
                    isset( $_POST[\'action\'] )
                    &&
                    $_POST[\'action\'] === \'editpost\'
                    &&
                    isset( $_POST[\'post_type\'] )
                    &&
                    isset( $_POST[\'post_ID\'] )
                    &&
                    strpos( wp_get_referer(), \'action=edit\' ) !== false
                ) {
                    // Submit Edit Post page
                    $result = true;
                }
            }
        }

        return $result;
    }
}
在编辑页面返回true,在其他页面返回false。。。

结束