• Paul Wright

    (@signyourbikeout)


    Hello,

    I am using a dual-layer navigation, with wp_list_categories making the upper nav and the following code for generating the lower navigation with the children in necessary:

    <?php
    		$cat = get_query_var('cat');
    		$category = get_category ($cat);
    
    		if ($category->cat_ID) {
    
    	if($category->category_parent)
    	$children = wp_list_categories("orderby=id&hide_empty=0&title_li=&child_of=".$category->category_parent."&echo=0");
    	else $children = wp_list_categories("orderby=id&hide_empty=0&title_li=&child_of=".$category->cat_ID."&echo=0");
    	if ($children) {
    	echo $children; }
            } 
    
       ?>

    First, if there are no children, it shows “no categories” in my lower navigation instead of showing nothing at all. How can I get it to return nothing in the case that there are no children?

    Second, I have the code in the header and it works very well for the archive based pages I have, but I lose the lower navigation when I go to single post view. How can I get the lower navigation to show up in single view?

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Paul Wright

    (@signyourbikeout)

    Any help on these would be greatly appreciated.

    For the first one, is it possible to do a str_replace or something like that, in order to just remove the words “no categories”?

    For the second question, how is my code not targeting the single page case? The lower navigation isn’t showing up when on single post view.

    Thanks!

    Thread Starter Paul Wright

    (@signyourbikeout)

    For those that are interested, I solved the first problem. It seems that WordPress outputs “No categories” for wp_list_categories if there are none. It counts that as having children…so even using a else statement with echo (”) for the case of no children did not work. So instead, I used str_replace to remove “No categories”.
    For those who didn’t know, str_replace is case sensitive.

    <div id="sub_navbar" class="clearfloat">
    <ul id="sub_page-bar">
    
     <?php
    $cat = get_query_var('cat');
    $category = get_category ($cat);
    
    if ($category->cat_ID) {
    
      if($category->category_parent)
      $children = wp_list_categories("orderby=id&hide_empty=0&title_li=&child_of=".$category->category_parent."&echo=0");
      else $children = wp_list_categories("orderby=id&hide_empty=0&title_li=&child_of=".$category->cat_ID."&echo=0");
    
    $children = str_replace('No categories', '', $children);
    
      if ($children) {
       echo $children;
      }
    }
    ?>
    
    </ul>
    </div>

    Anyone have a suggestion on how to get it to also work on single post view?

    Thanks

    Thread Starter Paul Wright

    (@signyourbikeout)

    OK, I have kept working and I was doing some trial and error with pieces of a few similar functions and while I was merging two functions and cleaning up the unneeded lines, this worked:

    <?php
    
    $categories = get_the_category();
     $this_cat_ID = $categories[0]->cat_ID;
     foreach ($categories as $cat) {
     }
    $category = get_category($cat);
     if ($category->cat_ID) {
      if($category->category_parent)
      $children = wp_list_categories("orderby=id&hide_empty=0&title_li=&child_of=".$category->category_parent."&echo=0");
      else $children = wp_list_categories("orderby=id&hide_empty=0&title_li=&child_of=".$category->cat_ID."&echo=0");
    
    $children = str_replace('<li>No categories</li>', '', $children);
    
    	if ($children) {
    	echo $children;
    			}
    	}
        ?>

    This correctly shows the children when on the parent category (archive) page, the child category (archive) page as well as showing the correct children when on the child’s single post view.

    My problem still is that I’m not exactly sure how this is working…it seems a bit delicate…at the very least it is not an elegant solution. Can someone help me clean it up… the concern I still have, is that when it is doing the wp_list_categories for the children on the single post view (only) it doesn’t add the “current-cat” class. The current-category-parent and current-cat is all working fine on the archive pages, and the current-category-parent is even working on the single post view..but on this view the “current-cat” for the children isn’t showing.

    Is it possible to do another str_replace that searches the result of wp_list_categories for the current category and replaces perhaps the

    • before it with <li class=”current-cat”>. Im not 100% sure how to use regular expressions to query the string in order to do the search and replace.
    • Thanks.

    Thread Starter Paul Wright

    (@signyourbikeout)

    I think the problem I am having with this latest piece of code is it looks at the posts from the loop when determining the children…I’m having some weirdness with it not showing exactly the correct children sometimes (I think it’s only when the posts have multiple categories assigned to them)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘dual navigation with children not showing in single view’ is closed to new replies.