只有当WP Admin是子页面a、子页面B或子页面C时,我才能运行一段代码



我知道类似的问题已经得到了回答-我读过其中的一些答案,但就我的一生而言,似乎我缺少了一些东西/"没有得到";。所以,开始吧。

我正在尝试为我从头开始创建的WP主题构建一个管理菜单。我已经创建了一些子页面,现在我想在enqueue.php中进行检查,以便只有当用户在其中一个页面上时才加载stylesheet.css,而不是在整个菜单上。据我所知,我应该做一个逻辑检查,检查当前显示的页面是否是我菜单中的页面a、B或C,并且只有在正确的情况下才加载样式表。

根据YouTube上关于主题构建的教程,我已经到了我的代码是这样的阶段:

function themename_load_admin_scripts( $hook ) {
echo $hook;
if ( ($hook != 'toplevel_page_A') || ($hook != 'toplevel_page_B') ) {
return;
}
wp_register_style( 'themename_admin', get_template_directory_uri() . '/css/themename.admin.css', array(), '1.0.0', 'all');
wp_enqueue_style('themename_admin');
wp_register_script( 'themename-admin-script', get_template_directory_uri() . '/js/themename.admin.js', array('jquery'), '1.0.0', true );
wp_enqueue_script( 'themename-admin-script' );
}
add_action( "admin_enqueue_scripts", 'themename_load_admin_scripts' );

啊,它不起作用。作为一个额外的问题,既然我想检查多个页面,而不仅仅是两个页面,那么如果我使用数组进行检查会更好吗?如果是。。。怎样

提前感谢您的帮助和建议。

是的,它很管用!

在@Pankaj告诉我如何用数组实现这一目标后,我研究了一下,实现了一个不同的方法,但它仍然不起作用。在确定这不是因为拼写错误、错误的值或诸如此类的事情之后,我想起了Firefox Nightly的一个非常令人讨厌的怪癖:有时,如果更新受阻,有些事情就会停止工作,原因我真的无法解释。瞧,还有另一个阻碍更新。我下载了它,重新启动了浏览器,然后神奇地自动一切正常。

请注意,这包括基于阵列的解决方案和我以前的解决方案。老实说,我不知道他们在Mozilla抽烟,也不知道每当有更新敲门时,Firefox是如何变得如此艰难的。

无论如何,要澄清的是,上面的代码应该都能工作——这是浏览器的错——以及新的基于阵列的实现,对于任何感兴趣的人来说都是如此:

<?php
/*
=========================================
Admin Enqueue Functions
=========================================
*/
function themename_load_admin_scripts( $hook ) {
echo $hook;
$themenameAdminpages = array('toplevel_page_themename_settings', 'themename_page_themename_style_and_looks', 'themename_page_themename_functionality', 'themename_page_themename_social', 'themename_page_themename_seo', 'themename_page_themename_about' );
if ( in_array($hook, $themenameAdminpages)) {
wp_register_style( 'themename_admin', get_template_directory_uri() . '/css/themename.admin.css', array(), '1.0.0', 'all');
wp_enqueue_style('themename_admin');
wp_register_script( 'themename-admin-script', get_template_directory_uri() . '/js/themename.admin.js', array('jquery'), '1.0.0', true );
wp_enqueue_script( 'themename-admin-script' );
}
}
add_action( "admin_enqueue_scripts", 'themename_load_admin_scripts' );

附言:我知道我用的名字很烂。正如我的";代码";。

相关内容

最新更新