列出类别并突出显示当前类别存档页面

我需要显示所有帖子类别的按钮。当我单击一个按钮时,它会链接到该特定类别的存档页面。有没有办法突出当前类别?


我的代码


<div>

    <?php foreach(get_categories($args) as $category) { ?>

            <a href="<?php echo get_category_link($category); ?>"><?php echo $category->name; ?></a>

    <?php } ?>

</div>


摇曳的蔷薇
浏览 37回答 1
1回答

繁星coding

请试试这个:<?php&nbsp;$selected_category = get_queried_object();$current_category = $selected_category->term_id;foreach(get_categories($args) as $category) {&nbsp;&nbsp; &nbsp; $selected_class = '';&nbsp; &nbsp; if( $category->term_id == $current_category ){&nbsp; &nbsp; &nbsp; &nbsp; $selected_class = "selected_a";&nbsp; &nbsp; }&nbsp; &nbsp; ?>&nbsp; &nbsp; <a href="<?php echo get_category_link($category); ?>" class="<?php echo $selected_class; ?>" ><?php echo $category->name; ?></a>&nbsp; &nbsp; <?php&nbsp;}&nbsp;?>然后,您可以为“selected_a”类添加背景 CSS。谢谢
打开App,查看更多内容
随时随地看视频慕课网APP