Answers:
这是我有的功能:
/**
* 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
}
is_admin
;)。还有更多吗?
我喜欢get_current_screen()
,因为它要简单得多:
$screen = get_current_screen();
if ($screen->base == "post") {
}
此函数返回一个对象,其中包括屏幕的ID,基本,帖子类型和分类以及其他数据点。
(法典)
我想enqueue script
和styles
只在特定的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;
}
}
要使用该功能,请传递帖子类型名称。
/**
* return true if 'edit' or 'new-post' screen of 'cpt_name' is opened
*/
if ( is_edit_or_new_cpt('cpt_name') ) {
// do something
}
我有这个功能
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。