• Hey all,

    I’m trying to create a custom navigation bar in my nav.php file, which will check a number of categories. If it is a cateogry with no sub-categories but has posts, it will start an unordered list and print the post titles belonging to that category. If the category has sub categories, it will print the sub category name and start another second level unordered list and proceed to print out the post titles of the sub-category.

    I have been successful in getting it to print out the nested unordered lists in exactly the way I want, however when I click on a post title, it links to the correct URL, but carries over the post ID from the last post included in the WP_Query in the nav.php, thus all the text and images are incorrect.

    I have thoroughly searched around, and was under the impression that a WP_Query is effectively terminated with the endwhile; and endif; statements?

    Here is the code as it appears in my nav.php. Any help would be really appreciated

    <ul><?php
    	$parentCatID = '';
    	$reqCategories=get_categories('include=2,3,4,5,6,7,8,9');
    	foreach($reqCategories as $category) {
    		// if category has no parent, then we want to print it
    		if ($category->category_parent == 0) {
    			// print primary category listings
    			echo "<li><a href=\"".get_category_link($category->cat_ID)."\" title=\"".$category->cat_name."\">".$category->cat_name."</a><ul>";
    			// setting current cateogry as the parent category
    			$parentCatID = $category->cat_ID;
    			// if this primary category has no sub-categories, but has posts, we want to display them
    			$primaryCatPosts = new WP_Query(array('cat__in' => array($parentCatID)));
    			if($primaryCatPosts->have_posts()) : while($primaryCatPosts->have_posts()) : $primaryCatPosts->the_post();
    				$postTitle = the_title('', '', FALSE);
    				$postLink = get_permalink();
    				echo "<li><a href=\"".$postLink."\" title=\"".$postTitle."\">".$postTitle."</a></li>";
    			endwhile; endif;
    			// and display the sub-category listing
    			$subCategories=get_categories('child_of='.$parentCatID);
    			foreach($subCategories as $category) {
    				echo "<li><a href=\"".get_category_link($category->cat_ID)."\" title=\"".$category->cat_name."\">".$category->cat_name."</a><ul>";
    				$subCatPosts = new WP_Query("cat=$category->cat_ID");
    				if($subCatPosts->have_posts()) : while($subCatPosts->have_posts()) : $subCatPosts->the_post();
    					$postTitle = the_title('', '', FALSE);
    					$postLink = get_permalink();
    					echo "<li><a href=\"".$postLink."\" title=\"".$postTitle."\">".$postTitle."</a></li>";
    				endwhile; endif;
    			echo "</ul></li>";
    			}
    		echo '</ul></li>';
    		}
    	} $parentCatID = '';
    					?></ul>

    To give me a rendered HTML output such as:

    <ul>
      <li>PARENT CAT 1
        <ul>
          <li>CHILD POST 1</li>
          <li>CHILD POST 2</li>
        </ul>
      </li>
      <li>PARENT CAT 2
        <ul>
         <li>SUB CAT 1
          <ul>
           <li>CHILD POST 3</li>
           <li>CHILD POST 4</li>
          </ul>
        </li>
        <li>SUB CAT 2
         <ul>
          <li>CHILD POST 5</li>
          <li>CHILD POST 6</li>
         </ul>
        </li>
       </ul>
      </li>
    </ul>
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘WP_Query in nav.php affecting single.php’ is closed to new replies.