Wordpress 如何在admin_init hook回调中检测自定义post类型?
或者我应该使用不同的钩子?主要是,我试图仅在管理中执行一些操作,并且仅对一些自定义帖子执行这些操作。此方法很简单:Wordpress 如何在admin_init hook回调中检测自定义post类型?,wordpress,Wordpress,或者我应该使用不同的钩子?主要是,我试图仅在管理中执行一些操作,并且仅对一些自定义帖子执行这些操作。此方法很简单: /** * Based on http://wordpress.stackexchange.com/a/59999/12615 */ add_action( 'admin_head', 'wpse_59652_list_terms_exclusions' ); function wpse_59652_list_terms_exclusions() { global
/**
* Based on http://wordpress.stackexchange.com/a/59999/12615
*/
add_action( 'admin_head', 'wpse_59652_list_terms_exclusions' );
function wpse_59652_list_terms_exclusions() {
global $current_screen;
if( 'post' != $current_screen->post_type )
return;
// Do your stuff
}
另一个:
/**
* Based on http://wordpress.stackexchange.com/a/54279/12615
*/
add_action( 'admin_notices', 'wpse_54258_display_error_message' );
function wpse_54258_display_error_message() {
global $post;
if( !isset( $post ) || 'page' != $post->post_type )
return;
?>
<div class="error fade">This is a "page" post type</div>
<?php
}
这里有一个函数,您可以毫无问题地使用它
function admin_post_type () {
global $post, $parent_file, $typenow, $current_screen, $pagenow;
$post_type = NULL;
if($post && (property_exists($post, 'post_type') || method_exists($post, 'post_type')))
$post_type = $post->post_type;
if(empty($post_type) && !empty($current_screen) && (property_exists($current_screen, 'post_type') || method_exists($current_screen, 'post_type')) && !empty($current_screen->post_type))
$post_type = $current_screen->post_type;
if(empty($post_type) && !empty($typenow))
$post_type = $typenow;
if(empty($post_type) && function_exists('get_current_screen'))
$post_type = get_current_screen();
if(empty($post_type) && isset($_REQUEST['post']) && !empty($_REQUEST['post']) && function_exists('get_post_type') && $get_post_type = get_post_type((int)$_REQUEST['post']))
$post_type = $get_post_type;
if(empty($post_type) && isset($_REQUEST['post_type']) && !empty($_REQUEST['post_type']))
$post_type = sanitize_key($_REQUEST['post_type']);
if(empty($post_type) && 'edit.php' == $pagenow)
$post_type = 'post';
return $post_type;
}
我让它灵活、快速、准确。你可以根据需要扩展它
function admin_post_type () {
global $post, $parent_file, $typenow, $current_screen, $pagenow;
$post_type = NULL;
if($post && (property_exists($post, 'post_type') || method_exists($post, 'post_type')))
$post_type = $post->post_type;
if(empty($post_type) && !empty($current_screen) && (property_exists($current_screen, 'post_type') || method_exists($current_screen, 'post_type')) && !empty($current_screen->post_type))
$post_type = $current_screen->post_type;
if(empty($post_type) && !empty($typenow))
$post_type = $typenow;
if(empty($post_type) && function_exists('get_current_screen'))
$post_type = get_current_screen();
if(empty($post_type) && isset($_REQUEST['post']) && !empty($_REQUEST['post']) && function_exists('get_post_type') && $get_post_type = get_post_type((int)$_REQUEST['post']))
$post_type = $get_post_type;
if(empty($post_type) && isset($_REQUEST['post_type']) && !empty($_REQUEST['post_type']))
$post_type = sanitize_key($_REQUEST['post_type']);
if(empty($post_type) && 'edit.php' == $pagenow)
$post_type = 'post';
return $post_type;
}