在自定义类别列表中显示类别ID

时间:2011-08-09 作者:juliusk

我创建了一个类别列表,以我的手风琴可以使用的方式显示它们。(侧栏中的自定义)我想为当前查看的帖子添加一个名为“current”的类,并且应该打开已查看的类别。您有什么想法可以将其集成到代码中吗?这是一项艰苦的工作,让这项工作,现在我不知道如何整合它。。。

链接:http://www.davidgoltz.de/2011/anna-bederke-actor/ (单击“公文包”后,将显示菜单,并应显示打开的树和突出显示的帖子。

代码如下:

<div id="category-list">
    <ul class="topnav"> 
        <?php
        $kategorien = get_categories(array(
        \'child_of\' => 0,
        \'exclude_tree\' => 15,
        \'exclude\' => 16,
        \'orderby\' => \'slug\',
        \'order\' => \'ASC\')); 
            foreach($kategorien as $kategorie) { 
            echo \'<li><a href="#" class="category-title">\'; echo ($kategorie->name); echo \'</a>\';
                echo \'<ul>\';
                    $cat = $kategorie->cat_ID;
                    $posts = get_posts(\'category=\'. $cat .\'&posts_per_page=-1\');
                        foreach($posts as $post) { ?>
                        <li>
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        <li>
                    <? }
                echo \'</ul>\';
        echo \'</li>\';
            } 
        ?>                  
    </ul>
</div>

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

Try this:

<div id="category-list">
    <ul class="topnav"> 
        <?php
        if (is_single()){
            global $post;
            $pid = $post->ID;
            $post_categories = wp_get_post_categories( $post->ID );
            $cats = array();
            foreach($post_categories as $c){
                $cat = get_category( $c );
                $cats[] = $cat->ID;
            }
            $active_cat_count = true;
            $cat_count = 0;
        }

        $kategorien = get_categories(array(
        \'child_of\' => 0,
        \'exclude_tree\' => 15,
        \'exclude\' => 16,
        \'orderby\' => \'slug\',
        \'order\' => \'ASC\')); 
            foreach($kategorien as $kategorie) { 
            if (isset($active_cat_count) && $active_cat_count){
                if (in_array($kategorie->ID,$cats){
                    $active_cat_count = false;
                }else{
                    $cat_count = $cat_count + 1;
                }
            }
            echo \'<li><a href="#" class="category-title">\'; echo ($kategorie->name); echo \'</a>\';
                echo \'<ul>\';
                    $cat = $kategorie->cat_ID;
                    $posts = get_posts(\'category=\'. $cat .\'&posts_per_page=-1\');
                        foreach($posts as $post) { ?>
                        <li>
                            <a <?php if ($post->ID == $pid){echo \'class="current"\';} ?> href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        <li>
                    <? }
                echo \'</ul>\';
        echo \'</li>\';
            } 
        ?>                  
    </ul>
</div>
<?php
if (is_single()){
?>
<script>
jQuery(document).ready(function(){
    $( ".topnav" ).accordion( "option", "active", <?php echo $cat_count; ?> );
});
</script>
<?php
}
结束