我正在建立一个网站,其中有一些带有子页面的页面,当我单击导航菜单中的父链接时,便被重定向到该页面。然后它将显示该页面上所有子页面的链接,我的问题是,即使单击子页面链接,我也希望导航菜单中的父页面链接始终突出显示。因此,最后应突出显示父页面链接和子页面链接。

我尝试了一些不同的方法,但似乎没有任何效果,我可以循环出父级的所有子级页面,但不能使其突出显示。

    <?php

     if ( $post->post_parent ) :
       $parent = $post->post_parent;
     else :
       $parent = $post->ID;
     endif;

     $childpages = get_pages('child_of=' . $parent .        '&sort_column=menu_order');

     ?>

    <div class="row pad60">
     <ul class="category-nav column">
       <?php foreach( $childpages as $childpage ) { ?>

            <a href="<?= get_page_link( $childpage->ID ) ?>">
                <li><div class="primary-btn"> <?= $childpage->post_title ?> </div></li>
            </a>

     <?php } // End foreach ?>
   </ul>
</div>


这就是我现在正在使用的方式,但是我觉得这首先不是正确的方法。

谢谢!

最佳答案

WordPress在CSS类中构建了一个以菜单为目标的版本。 https://developer.wordpress.org/reference/functions/wp_nav_menu/#Menu_Item_CSS_Classes

// This will highlight all the current menus
.current-menu-item a {
    color: blue;
    }

/* This will highlight its parent, when you are in its child page. */
.current_page_parent a {
    color: orange;
    }

07-24 09:47
查看更多