Help with ACTIVE state list CATEGORIES and POST TITLES
-
Hi,
Right now I’m working on a website completely based on CATEGORIES and CUSTOM POST TYPES. Normally I would make a static menu that links to the correct SINGLE and CATEGORY templates for naviagtion. This time I’m looking for a generated menu based on all CATEGORIES and the posts that are connected to them.The menu I have now is up and running, creates a tree system of all posts and categories and is really easy to style by CSS. The only thing that I haven’t got to work is attaching and ACTIVE class to the current post TITLE.
I’m trying to do this by checking if the $post->ID of the current post is the same as that of the post TITLE.
The code I have now show the correct $post->ID of the current post TITLE but I haven’t been able to send it send it down to the “$post->ID == $IDOutsideLoop” part. When I put the correct $post->ID in myself everything, it works fine.
I’ve been trying to echo $IDOutsideLoop everywhere inside the code but it seems the $post->ID of the current post seems to get lost after the first endforeach. Even removing the “wp_reset_query();” gives no answers..
Can somebody help me completing this menu by help solving this problem?
<div id="auto-menu"> <?php $IDOutsideLoop = $post->ID; echo $IDOutsideLoop; foreach( get_categories('hide_empty=0&include=6') as $cat ) : if( !$cat->parent ) { echo '<ul><span id="auto-menu-hide"><li>' . $cat->name . '</li></span><div>'; process_cat_tree( $cat->term_id ); } endforeach; wp_reset_query(); //to reset all trouble done to the original query // function process_cat_tree( $cat) { $args = array('category__in' => array( $cat ), 'numberposts' => -1); $cat_posts = get_posts( $args ); if( $cat_posts ) : foreach( $cat_posts as $post ) : echo $IDOutsideLoop; echo '<li'; if ( $post->ID == $IDOutsideLoop ) { echo ' class="active"'; } else {} echo '>'; echo '<a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a>'; echo '</li>'; endforeach; endif; $next = get_categories('hide_empty=0&parent=' . $cat); if( $next ) : foreach( $next as $cat ) : echo '<ul><span id="' . $cat->slug . '-header"><li><h2>' . $cat->name . '</h2></li></span><div id="' . $cat->slug . '-box">'; process_cat_tree( $cat->term_id ); endforeach; endif; echo '</div></ul>'; } ?> </div>
-
Got it working!
Thanks to knottulf.
Here is the working code:
<div id="auto-menu"> <?php foreach( get_categories('hide_empty=0&include=16') as $cat ) : if( !$cat->parent ) { echo '<ul><span id="auto-menu-hide"><li>' . $cat->name . '</li></span><div>'; process_cat_tree( $cat->term_id ); } endforeach; wp_reset_query(); //to reset all trouble done to the original query // function process_cat_tree( $cat) { $args = array('category__in' => array( $cat ), 'numberposts' => -1); $cat_posts = get_posts( $args ); global $post; if( $cat_posts ) : foreach( $cat_posts as $menuPost ) : echo '<li'; if ( $menuPost->ID == $post->ID ) { echo ' class="active"'; } echo '>'; echo '<a href="' . get_permalink( $menuPost->ID ) . '">' . $menuPost->post_title . '</a>'; echo '</li>'; endforeach; endif; $next = get_categories('hide_empty=0&&parent=' . $cat); if( $next ) : foreach( $next as $cat ) : echo '<ul><span id="' . $cat->slug . '-header"><li><h2><a href="http://url/site/' . $cat->slug . '">' . $cat->name . '</a></h2></li></span><div id="' . $cat->slug . '-box" class="box-closed">'; process_cat_tree( $cat->term_id ); endforeach; endif; echo '</div></ul>'; } ?> </div>
- The topic ‘Help with ACTIVE state list CATEGORIES and POST TITLES’ is closed to new replies.