0

I created a category-list to show them in a way, my accordion can use. (Custom in the sidebar) I want to add a class named "current" for the currently viewed post and the viewed category should be opened. Do you have any ideas to integrate this in the code? It was hard work to get this to work and now I dont know how to integrate it...

Link: http://www.davidgoltz.de/2011/anna-bederke-actor/ (after you click "Portfolio", the menu appears and should show the opened tree with a highlighted post.

Here is the code:

<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 Answer 1

0

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
}
12
  • Link: davidgoltz.de/2011/anna-bederke-actor Mh, doesnt work! Whats this at the end? <?php }
    – juliusk
    Commented Aug 9, 2011 at 10:54
  • the <?php } at the end is to close the final if statement, as for why its not working' i can't see the code there so i can't tell. let me know when you use that code and i'll take a look.
    – Bainternet
    Commented Aug 9, 2011 at 13:06
  • Its online now (previous link)!
    – juliusk
    Commented Aug 9, 2011 at 15:05
  • I don't see the JavaScript is this a custom post type or a regular single post view?
    – Bainternet
    Commented Aug 9, 2011 at 15:39
  • Its a custom post type: Heres the javascript $(document).ready(function() { $(".topnav").accordion({ header: 'a.category-title', accordion:true, speed: 500, closedSign: '+', openedSign: '-' }); });
    – juliusk
    Commented Aug 10, 2011 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.