我有一个自定义侧边栏,它由当前页面的同级列表填充。我想向此列表中的当前页面添加一个类,以便可以在列表中突出显示它。我创建侧边栏的方式如下:
<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-->我正在尝试找出向侧边栏列表中的当前页面添加类的最简单方法。任何帮助都将不胜感激!
发布于 2015-05-12 22:22:01
使用JS可以获取当前页面的URL,然后在文档(当前页面)中找到锚定标记,并添加类('active')或任何你想要的类
$(window).load(function () {
var pathname = window.location.pathname;
$("a[href$='" + pathname + "']").addClass('yourClassNameHere');
});发布于 2015-05-12 22:24:15
你可以在你的body标签中使用body_class(),例如。
<body <?php body_class(); ?>>它将根据当前页面生成唯一的主体类:
<body class="page page-id-2 page-parent page-template-default logged-in">然后在css中,你可以让你的选择器
.page-id-2 .side_bar_global_class { /* Rules here */ }https://stackoverflow.com/questions/30193588
复制相似问题