在兄弟姐妹列表中将类添加到当前页面-WordPress



我有一个自定义侧边栏,该侧边栏由当前页面的兄弟姐妹列表填充。我想在此列表中的当前页面中添加一个类,以便可以在列表中重点介绍它。我正在创建这样的侧边栏:

                      <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,然后在文档(当前页面)中找到锚标记,然后添加类('active')或您想要的任何类

$(window).load(function () {
    var pathname = window.location.pathname;
    $("a[href$='" + pathname + "']").addClass('yourClassNameHere');
});

您可以在身体标签中使用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 */ }

最新更新