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

mpbci0fu  于 2023-04-28  发布在  PHP
关注(0)|答案(2)|浏览(137)

我需要显示所有职位类别的按钮。当我点击一个按钮时,它会链接到该特定类别的存档页面。是否有方法突出显示当前类别?

我的密码

<div>
    <?php foreach(get_categories($args) as $category) { ?>
            <a href="<?php echo get_category_link($category); ?>"><?php echo $category->name; ?></a>
    <?php } ?>
</div>
vawmfj5a

vawmfj5a1#

请试试这个:

<?php 

$selected_category = get_queried_object();
$current_category = $selected_category->term_id;

foreach(get_categories($args) as $category) { 

    $selected_class = '';
    if( $category->term_id == $current_category ){
        $selected_class = "selected_a";
    }

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

然后,您可以为“selected_a”类添加背景CSS。谢谢

efzxgjgh

efzxgjgh2#

你也可以试试这个。

$categories = get_categories( array(
              'orderby' => 'name',
              'order'   => 'ASC'
            ) );
    foreach( $categories as $key=>$category ) :
     $catLink = get_category_link( $category->term_id ); 
     $classactive = ( is_category( $category->name ) ) ? 'active' : '';
    ?>
    <li class="<?=$classactive?>">
        <a href="<?=$catLink?>"><?= $category->name  ?> 
            <span><?= $category->count ?></span>
       </a>
    </li>
    <?php endforeach; ?>

相关问题