【问题标题】:add class to element based on custom page (Wordpress)根据自定义页面向元素添加类(Wordpress)
【发布时间】:2020-07-03 02:45:06
【问题描述】:

目标: 我需要根据当前活动页面突出显示菜单/导航链接上的元素。示例 - 查看“关于”页面将突出显示菜单上的“关于”项。

我尝试过的: highlight active element based on active page 最有意义,但是:

  • 它不是根据正在查看的页面添加类。我在 .php 模板文件和从仪表板本身创建的 Wordpress 页面上添加了变量,但两者都不起作用。

关于如何完成此任务的任何建议?

Wordpress 仪表板页面:关于 自定义模板文件:About.php

菜单项:

<div>
  <a class="(add class here based on page url)">About</a>
  <a class="(add class here based on page url)">Blog</a>
  <a class="(add class here based on page url)">Products</a>
  <a class="(add class here based on page url)">Contact</a>
<div>

【问题讨论】:

  • 欢迎来到 SO!请参阅How to ask。 “它不起作用”——你到底在做什么,你期待什么,会发生什么?

标签: php wordpress


【解决方案1】:

你可以试试下面的:

<?php 
global $post;       
// add 'post_name' to the $post_class                
$post_class = $post->post_name;
?>

<div>
    <a class="<?php echo $post_class; ?>_page">item</a> 
    <a>item</a> 
    <a>item</a> 
    <a>item</a> 
<div>

$post_class 上,您将获得当前页面名称,因此类将基于页面名称获得。

谢谢!

【讨论】:

  • 我不太确定这就是 OP 的想法。我认为它是一个类,但应用于与当前页面对应的锚。
  • 如果你的页面 slug 是 "about" 你会像这样返回类:about_page
猜你喜欢
  • 2011-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多