阻止Wordpress管理菜单中的顶级菜单链接起作用 [英] Prevent top-level menu link in Wordpress admin menu from working
问题描述
我要实现的目标.
我创建了一个自定义的顶级Wordpress菜单,并向其中添加了许多子菜单项.
I have created a custom Top-level Wordpress menu and added a number of sub-menu items to it as well.
我实际上不需要顶层项目的页面-这只是我想要作为将子菜单项链接到的标题"而存在的页面.
I don't actually need a page for the Top-level item - this is just something which I want to there as a 'heading' to hook the sub-menu items to.
理想情况是,因此,当您将光标移到顶层项目时,子菜单会弹出(默认情况下会弹出),但是如果单击它,则什么也不会发生.
Ideally i'd like it so when you move your cursor onto the top-level item, the sub-menu flies out (which it does by default) but if you click on it nothing happens.
有效的解决方案
此jQuery现在可以正常工作-如果您遇到麻烦,只需确保正确清除浏览器缓存即可.
This jQuery now works - just make sure you clear your browser cache properly if you are struggling.
// Javascript
jQuery(document).ready(function() {
jQuery('a.toplevel_page_menu_slug').click(function(event) { // where 'menu_slug' is your menu slug
/*alert ('working'); - Testing purposes only */
event.preventDefault(); // cancel the event
});
});
此CSS将鼠标悬停时的光标更改为默认光标,而不是指针.
And this CSS changes the cursor on mouse over to be the default, not the pointer.
//CSS
a.toplevel_page_menu_slug {
cursor: default;
}
推荐答案
很明显,您的脚本未加载或选择器不起作用.这是您应该如何在WP Admin中包含脚本的方法:
Obviously your script is either not loaded or the selector is not working. This is how you should include your script in WP Admin:
function preventTopLevelLink() {
wp_enqueue_script( 'prevent_top_level_link', get_template_directory_uri() . '/myscript.js' );
}
add_action( 'admin_enqueue_scripts', 'preventTopLevelLink' );
https://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
您的脚本基本上应该可以工作,请尝试如下操作:
Your script should basically work, try it like this:
(function($) {
$(document).ready(function() {
$(document).on('click','a.toplevel_page_menu_slug',function(event) { // where 'menu_slug' is your menu slug
event.stopPropagation();
event.preventDefault(); // cancel the event
});
});
})(jQuery);
这篇关于阻止Wordpress管理菜单中的顶级菜单链接起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!