Wordpress如何使用get_current_screen和全局pagenow

Wordpress how to use get_current_screen and global pagenow

不明白下面两个的区别

get_current_screen

pagenow

你能帮我吗?

根据get_current_screenDocs

it'll "return current screen object or null when screen not defined".

如果你想用它来检测admin pages,那么使用不同的钩子,例如你可以使用current_screen钩子:

add_action('current_screen', 'detecting_current_screen');

function detecting_current_screen()
{
  $current_screen = get_current_screen();

  print_r($current_screen);
}

或者

add_action('current_screen', 'detecting_current_screen');

function detecting_current_screen()
{
  global $current_screen;

  print_r($current_screen);
}

如果您想在 client-side 和 returns null 上使用它,那么您可以使用名为 $pagenow.

的全局变量

注意: $pagenow 会给你模板名称。

global $pagenow;

echo $pagenow;

或者

echo $GLOBALS["pagenow"];

这应该为您提供您所在模板的名称:

add_action( 'wp_footer', function () {

    global $pagenow;

    echo $pagenow;

}, 999 );

如果上面提到的 none 方法对您有用,那么您可以使用另一个名为 $post.

的全局变量

或者

您可以使用以下方式检查您的页面:

get_page_by_titleDocs
or
get_page_by_pathDocs