如何在类别和单页模板上突出显示当前类别?

时间:2014-01-24 作者:Omur U

在Wp论坛上,我发现了一个用图像显示类别的代码,它工作得很好。但我不知道如何在类别上突出显示当前类别。php和单个。php

这是我正在使用的代码

$cats = get_categories(\'hide_empty=0&child_of=2&orderby=count&number=99&order=asc\');
foreach ((array)$cats as $cat) {
$catdesc = $cat->category_description;
echo \'<a href="\' . get_category_link($cat) . \'" title="\'. strip_tags($catdesc) .\'">
        <li>
          <img src="\' . get_home_url () . \'/wp-content/uploads/\' . $cat->slug. \'.jpg" alt="\' . $cat->cat_name . \'" class="front-img" />
        </li>
      </a>\';
}

1 个回复
SO网友:Nickolay Ninarski

你可以这样做

    <?php
        $current_category = get_the_category();
        /* 
           $current_category[0]->ID is holding the current category now. 
           If there are more than 1 categories for the post you should iterate
           through all of them and check if the current category you\'re 
           displaying is in there
        */

        $cats = get_categories(\'hide_empty=0&child_of=2&orderby=count&number=99&order=asc\');
        foreach ((array)$cats as $cat) {
            $catdesc = $cat->category_description;
            ?>
            <li class="<?php echo ( ( $current_category[0]->ID === $cat->ID )? \'current\' : \'\' ); ?>">
                <a href="<?php echo get_category_link($cat); ?>" title="<?php echo strip_tags($catdesc); ?>">
                    <img src="<?php echo get_home_url (); ?>/wp-content/uploads/<?php echo $cat->slug ?>" alt="<?php echo $cat->cat_name; ?>" class="front-img">
                </a>
            </li>
        <?php } ?>

结束

相关推荐

Load Pages Menu in single.php

我搜索了很多,但没有找到答案。。。我的Wordpress有4个主要区域,每个区域还有一个新闻(帖子)部分和一个子菜单。如果我加载一篇文章(使用single.php),是否可以将相应的子菜单加载到该区域?我正在考虑对段塞进行分串,以获得正确的区域。非常感谢您的帮助。Thx,奥利弗其他信息:有一个侧边栏,我也会在单曲中显示。php侧边栏应包含一个区域的菜单树,贴子未连接到菜单