Advertisement
pjerky

Nested Wordpress Menu excluding duplicate posts.

Nov 11th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. <?php
  2. $url = explode('/', $_SERVER['REQUEST_URI']);
  3. $dir = $url[2] ? $url[2] : 'UX-Style-Guide';
  4. $result = str_replace ("?","",$dir);
  5. $final = current(explode('=', $result));
  6. $style = str_replace ("-"," ",$final);
  7. $style = ucwords($style);
  8. $post_id = $post->ID;
  9. $class = '';
  10.  
  11. function displayPosts($categoryObj, &$displayedPosts, $post_id, $final) {
  12.   $nav_post_args = array(
  13.     'numberposts' => -1,
  14.     'category' => $categoryObj->term_id,
  15.     'post_type' => $final,
  16.     'orderby' => 'name',
  17.     'order' => 'ASC',
  18.     'child_of' => 0  
  19.   );
  20.  
  21.   $nav_posts = get_posts($nav_post_args);
  22.   $output = '';
  23.  
  24.   foreach($nav_posts as $post) {
  25.     if (in_array($post->ID, $displayedPosts)) {
  26.       continue; //Skip this post if it has already been displayed once.
  27.     }
  28.  
  29.     $title = the_title();
  30.     $link = the_permalink();
  31.  
  32.     if ($title === '→') {
  33.       continue; //Help avoid output weirdness
  34.     }
  35.  
  36.     $displayedPosts[] = $post->ID;
  37.     $class = '';
  38.  
  39.     if ($post_id == $post->ID) {
  40.       $class = 'entry-title current';
  41.     } else {
  42.       $class = 'entry-title';
  43.     }
  44.  
  45.     $output .= "<li class='$class'>";
  46.     $output .= "<a class='entry-link' href='$link'>$title</a>";
  47.     $output .= "</li>\n";
  48.   }
  49.  
  50.   return $output;
  51. }
  52. ?>
  53.  
  54. <!-- THIS IS THE LEFTNAV -->
  55. <nav id="verticalNav" class="ui-side-nav-drawer">
  56.   <div class="cssmenu">
  57.     <div id="entry-search">
  58.       <input type="text" placeholder="Search Styleguide Entries" class="fuzzy-search" />
  59.     </div>
  60.     <ul class="list">
  61.       <?php
  62.         $nav_cat_args = array(
  63.           'orderby' => 'name',
  64.           'order' => 'ASC',
  65.           'child_of' => 0,
  66.           'exclude'=> 1
  67.         );
  68.  
  69.         $nav_categories = get_categories($nav_cat_args);
  70.         $displayedPosts = array();
  71.  
  72.         foreach($nav_categories as $category) {
  73.           echo "<li><span class='navAppTitle'>" . $category->name . "</span></li>\n";
  74.           echo displayPosts($category, $displayedPosts, $post_id, $final);
  75.  
  76.           $subcatArgs = array('parent' => $category->ID);
  77.           $subCategories = get_categories($subcatArgs);
  78.  
  79.           foreach ($subCategories as $subCategory) {
  80.             echo "<ul class='subCategoryList'>\n";
  81.             echo "<li><span class='navAppTitle subCategory'>" . $subCategory->name . "</span></li>\n";
  82.             echo displayPosts($subCategory, $displayedPosts, $post_id, $final);
  83.             echo "</ul>\n";
  84.           }
  85.         }
  86.       ?>
  87.  
  88.       <?php wp_reset_query(); ?>
  89.  
  90.       <div id="job-listings"></div>
  91.     </ul>
  92.   </div>
  93.  
  94. </nav><!-- /END #verticalNav -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement