問題描述

這似乎很簡單,但是我需要確定當前屏幕是添加新的還是編輯的 (一種 wordpress 管理條件標籤) 。有沒有一個內置的功能已經,或… 任何想法如何完成?

最佳解決方案

這是我有一個功能:

/**
 * is_edit_page
 * function to check if the current page is a post edit page
 *
 * @author Ohad Raz <admin@bainternet.info>
 *
 * @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' ) );
}

使用的用法很簡單,就像任何其他條件標籤一樣,幾個例子:

檢查新的或編輯頁面:

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 new 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
}

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。