无法在 PHP 中为 WordPress 网站设置列表项类
我正在寻找设置列表项的 currentmenu 类的最佳方法,以便向用户显示使用 php 选择的当前活动菜单。
我已将我的 html/css 网站移至 WordPress,但最初将所有代码都放在我的 index.html 文件中,该文件包含 sidemenu div 和用于在活动菜单选择之间切换的 jQuery 处理。
我最初在index.html 中的代码是:
使用Superfish 的侧边栏菜单的HTML 代码:
<div id="sidebar">
<ul id="themenu" class="sf-menu sf-vertical">
<li><a href="index.php" class="topm currentMenu nosub">Home</a></li>
<li><a href="about-us.php" class="topm nosub">About Us</a></li>
</ul>
</div>
用于切换当前活动菜单的jQuery 代码:
$("ul.sf-menu li a").click(function() {
$("ul.sf-menu li a").not(this).removeClass("currentMenu");
$(this).toggleClass("currentMenu");
});
由于这一切都在一个index.html 文件中,因此一切正常。
现在,随着迁移到 WordPress,将我的侧边栏菜单 div 移动到它自己的 sidebar.php 文件中,除此之外,还为 about-us.php 创建了单独的自定义 wp 页面,该页面调用 sidebar.php 文件,我有发生以下问题,这是我想确定解决问题的最佳方法的地方。
问题是,当用户第一次进入网站时,index.php 文件会触发,并且根据上述内容,“主页”菜单当前是基于默认类值的活动菜单。但是,当用户按下“关于我们”菜单选项(然后调用我创建的 about-us.php 页面,该页面也调用 sidebar.php 文件)时,当前活动菜单仍然是“主页”菜单选项,即不正确。
现在可以将“关于我们”菜单选项设置为活动菜单吗,即现在将“CurrentMenu”类设置为“关于我们”,因为我不确定如何在 php 中处理此问题,因为我的 jQuery 切换代码不再起作用?
我 我建议使用自己的 WordPress 页面模板调用 sidebar.php 文件来提供额外的菜单选项,因此需要将这些菜单选项设置为 class="currentMenu"。
I am looking for the best way to set a list item's currentmenu class, in order to show the user the current active menu selected using php.
I have moved my html/css site over to WordPress but originally had all my code inside my index.html file that consisted of a sidemenu div and jQuery processing to toggle between active menu selections.
The code I originally has in my index.html is:
HTML Code for Sidebar Menu using Superfish:
<div id="sidebar">
<ul id="themenu" class="sf-menu sf-vertical">
<li><a href="index.php" class="topm currentMenu nosub">Home</a></li>
<li><a href="about-us.php" class="topm nosub">About Us</a></li>
</ul>
</div>
jQuery Code for Toggling Current Active Menu:
$("ul.sf-menu li a").click(function() {
$("ul.sf-menu li a").not(this).removeClass("currentMenu");
$(this).toggleClass("currentMenu");
});
As this was all in the one index.html file, all worked fine.
Now with the move to WordPress and so moving my sidebar menu div into its own sidebar.php file and in addition to this, having created individual custom wp page for about-us.php which calls in the sidebar.php file, I have the following issue occurring, which is where I want to determine the best means to solve my issue.
The issue is, when the user first comes into the site, the index.php file fires and based on the above, the "Home" menu is currently the active menu based on default class value. But when the user presses the "About Us" menu option, which then calls my about-us.php page I created, that also calls the sidebar.php file, the current active menu is still the "Home" menu option, which is not correct.
Ho can I make the "About Us" menu option now the active menu, i.e. have the class "CurrentMenu" now set for About Us" as I am not sure how to approach this in php as my jQuery toggling code no longer works?
I am proposing to have additional menu options with their own wordpress page templates calling the sidebar.php file and so will need to have those menu option set as class="currentMenu".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您正在寻找 WordPress 条件标签。例如:
第一个导航行检查当前页面是否是主页,如果是,则打印菜单所需的标签。第二行检查该页面是否是“about-us”页面,其中“about-us”是 post_name (slug)。您还可以使用帖子 ID。有关可用相关标签的更多示例/参考,请查看下面的链接。希望这有帮助。
参考:http://codex.wordpress.org/Conditional_Tags
参考:http://codex.wordpress.org/Dynamic_Menu_Highlighting
要在标准 PHP 中执行此操作,请避免使用wordpress 作为平台,你可以这样做,或者直接分配特定页面的变量:
Sounds like you are looking for wordpress conditional tags. For example:
The first nav line checks to see if the current page is the homepage, if so it prints the required tag for the menu. The second line checks to see if the page is the "about-us" page, where "about-us" is the post_name (slug). You could also use the post ID. For more examples/references of available relevant tags, check my link below. Hope this helps.
Reference: http://codex.wordpress.org/Conditional_Tags
Reference: http://codex.wordpress.org/Dynamic_Menu_Highlighting
To do this in standard PHP, avoiding using wordpress as platform, you could do something like this, or just assign a variable of the specific pages directly:
为了将特定的类添加到侧边栏菜单(或任何菜单)的当前页面,您需要阅读 WordPress 文档中的动态菜单突出显示。您还可以尝试使用 dTabs 等插件 让事情变得更轻松。
In order to add a specific class to the current page in your sidebar menu (or any menu), you'll want to read up on dynamic menu highlighting in the WordPress documentation. You could also try a plugin such as dTabs to make things easier on yourself.