Php 将类添加到同级列表中的当前页面-Wordpress
我有一个自定义侧栏,由当前页面的同级列表填充。我想在列表中的当前页面中添加一个类,以便在列表中突出显示它。我正在创建这样的侧边栏:Php 将类添加到同级列表中的当前页面-Wordpress,php,html,wordpress,wp-list-categories,Php,Html,Wordpress,Wp List Categories,我有一个自定义侧栏,由当前页面的同级列表填充。我想在列表中的当前页面中添加一个类,以便在列表中突出显示它。我正在创建这样的侧边栏: <div class="col-sm-3 col-sm-offset-1 button-column"> <?php $siblingArgs = [
<div class="col-sm-3 col-sm-offset-1 button-column">
<?php
$siblingArgs = [
'child_of' => $post->post_parent,
'hierarchical' => 0,
'parent' => $post->post_parent,
];
$siblingPages = get_pages($siblingArgs);
$currentChildren = get_pages('child_of='.$post->ID);
foreach ($siblingPages as $siblingPage) : ?>
<a href="<?php echo $siblingPage->guid ?>" class="body-button"><?php echo $siblingPage->post_title ?></a>
<?php if($siblingPage->ID == $post->ID && (!empty($currentChildren))) {
echo '<ul class="hidden-list hidden-box">';
foreach ($currentChildren as $child){
echo '<li><a href="';
echo $child->guid.'">';
echo $child->post_title.'</a></li>';
};
echo '</ul>';
};
endforeach; ?>
</div><!--end column-->
我试图找出最简单的方法,将类添加到侧边栏列表中的当前页面。任何帮助都将不胜感激 使用JS,您可以获取当前页面URL,然后在文档(当前页面)中找到锚定标记,并添加类(“活动”)或任何您想要的类
$(window).load(function () {
var pathname = window.location.pathname;
$("a[href$='" + pathname + "']").addClass('yourClassNameHere');
});
你可以在你的身体标签中使用
<body <?php body_class(); ?>>
请缩进你的代码,这样它就离右边不远了。谢谢你的回复,不过我正在一个400页的网站上工作,所以这将是400个CSS声明。我正在寻找一种方法来动态地将PHP中的类添加到当前页面的同级中
<body class="page page-id-2 page-parent page-template-default logged-in">
.page-id-2 .side_bar_global_class { /* Rules here */ }