首页 > 解决方案 > Wordpress 如何使用 get_current_screen 和全局 pagenow

问题描述

我不明白以下两者之间的区别

获取当前屏幕

主页

你能帮我解决这个问题吗?

标签: phpwordpresscustom-wordpress-pages

解决方案


根据get_current_screen文档

它将“在未定义屏幕时返回当前屏幕对象或 null”。

如果你想用它来检测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返回 null 的 and 上使用它,那么你可以使用一个名为 的全局变量$pagenow

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

global $pagenow;

echo $pagenow;

或者

echo $GLOBALS["pagenow"];

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

add_action( 'wp_footer', function () {

    global $pagenow;

    echo $pagenow;

}, 999 );

如果上面提到的方法都不适合您,那么您可以使用另一个名为$post.

或者

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

get_page_by_title文档

get_page_by_path文档


推荐阅读