简体   繁体   English

使用 JQuery 隐藏<li>基于类名的主菜单元素

[英]Using JQuery to hide <li> element of main menu based on its class name

On a wordpress website, I am using the following code inside the head section of header.php to check if users are logged in (not in wordpress admin but a separate login area) and if they are not, I would like to hide a specific menu item (using the li element's class name with JQuery):在 wordpress 网站上,我在header.php的 head 部分中使用以下代码来检查用户是否已登录(不是在 wordpress 管理中,而是在单独的登录区域中),如果没有,我想隐藏特定的菜单项(在 JQuery 中使用li元素的类名):

<?php

if (isset($_COOKIE["username"])) {  

    // Do all relevant code for logged in users here

    }

else {
?>
<script type="text/javascript">

 $(document).ready(function() {        
    $('#menu-main-navigation-1 li.menu-item-123').hide();        


 });      

</script>
<?php
    // Do all relevant code for logged-out users here  

    }
?> 

The ul element's id is menu-main-navigation-1 and the class of the li that needs to be hidden is menu-item menu-item-type-custom menu-item-object-custom menu-item-123 ul元素的id是menu-main-navigation-1 ,需要隐藏的li类是menu-item menu-item-type-custom menu-item-object-custom menu-item-123

I have tried both with $(document).ready(function() and without but it didn't make any difference, the menu item still shows up.我已经尝试过$(document).ready(function()和没有,但没有任何区别,菜单项仍然显示。

Calling hide is not a very good idea.调用hide不是一个好主意。 It only changes the item's visibility but it will be still present in the source.它只会更改项目的可见性,但它仍将存在于源中。 If you absolutely have to do it in javascript, call remove , but this method still doesn't account for those users who have it disabled.如果您绝对必须在 javascript 中执行此操作,请调用remove ,但此方法仍然不考虑禁用它的用户。

But fortunately, you can remove elements from the wordpress menu by hooking before the html generation:但幸运的是,您可以通过在 html 生成之前挂钩来从 wordpress 菜单中删除元素:

 function filter_remove_menu_item_for_anons($menu_items, $args) {
    foreach($menu_items as $k => $item) {
      if ($item->ID == 123) {
        unset($menu_items[$k]);
      }
    }
    return $menu_items;
  }
  add_filter('wp_nav_menu_objects', 'filter_remove_menu_item_for_anons', 10, 2);

Just register the filter based on your session checks.只需根据您的会话检查注册过滤器。

If you have several menus and need finer control, you can do additional checks with the args parameter.如果您有多个菜单并需要更精细的控制,您可以使用args参数进行额外检查。

However, if the user knows the URL, he will still be able to access the content, and you should consider other solution .但是,如果用户知道 URL,他仍然可以访问内容,您应该考虑其他解决方案

Another approach ... you can also use CSS to hide the list item - which is probably better.另一种方法......你也可以使用 CSS 来隐藏列表项 - 这可能更好。

<?php
  if (isset($_COOKIE["username"])) {

  } else {
?>

   <style>
      #menu-main-navigation-1 li.menu-item-123 { display: none !important; }
   </style>

<?php
    }
?> 

Note: if they disable CSS, the list will show up.注意:如果他们禁用 CSS,则会显示该列表。 The original approach, if you disable JavaScript... same thing.原来的方法,如果你禁用 JavaScript ......同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM