logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$35
Help with wordpress category navigation

I am trying to create a website that is navigated by categories.
My categories are 3 levels deep;

Shoes (main category)

- Kids
--Sneakers
- Men
--Dress Shoes
- Women
--Heels

I would like when you click shoes(main category) you get:

- Kids
--Sneakers
- Men
--Dress Shoes
- Women
--Heels

and if you click on kids (level 1) you get:

- Kids (current)
--Sneakers
- Men
--Dress Shoes
- Women
--Heels

and if you click on sneakers (level 2) you get:

- Kids
--Sneakers (current)
- Men
--Dress Shoes
- Women
--Heels

My current code is:

<?php
if (is_category()) {
$this_category = get_category($cat);
}
?>
<?php
if($this_category->category_parent)
$this_category = wp_list_categories('orderby=title&hide_empty=0&show_count=0&depth=1&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent."&echo=0"); else
$this_category = wp_list_categories('orderby=title&hide_empty=0&show_count=0&depth=2&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if ($this_category) { ?>

<ul>
<?php echo $this_category; ?>

</ul>

<?php } ?>

This question has been answered.

xhanubis | 12/07/11 at 7:39pm Edit


(4) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    12/07/11
    7:43pm
    Gabriel Reguly says:

    Hi xhanubis,

    You mean you want a menu, with links to navigate, that is correct?

    Regards,
    Gabriel

    • 12/07/11 7:45pm

      xhanubis says:

      Yes, the different levels will show the products in the specific categories. The problem with my code above is that on level 1 (eg. kids) it just displays kids men and women in the sidebar. and on level 2 (eg. sneakers) it just display the sneakers and its siblings.

    • 12/07/11 8:09pm

      Gabriel Reguly says:

      Hi,

      Please try this code:


      <?php
      if (is_category()) {
      $this_category = get_category($cat);
      }
      ?>
      <?php
      if($this_category->category_parent)
      $this_category = wp_list_categories('orderby=title&hide_empty=0&show_count=0&depth=3&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent."&echo=0");
      else
      $this_category = wp_list_categories('orderby=title&hide_empty=0&show_count=0&depth=3&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
      if ($this_category) {
      ?>
      <ul>
      <?php echo $this_category; ?>
      </ul>
      <?php
      }
      ?>


      Regards,
      Gabriel

    • 12/07/11 8:12pm

      Gabriel Reguly says:

      Hi,

      Sorry, posted some wrong code.

      Please try this one instead:

      <?php
      if (is_category()) {
      $this_category = get_category($cat);
      }
      ?>
      <?php
      if($this_category->category_parent)
      $this_category = wp_list_categories('orderby=title&hide_empty=0&show_count=0&depth=0&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent."&echo=0");
      else
      $this_category = wp_list_categories('orderby=title&hide_empty=0&show_count=0&depth=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
      if ($this_category) {
      ?>
      <ul>
      <?php echo $this_category; ?>
      </ul>
      <?php
      }
      ?>


      Regards,
      Gabriel

    • 12/07/11 8:15pm

      xhanubis says:

      Hey Gabriel,

      That only works partially, I can now see the child and grandchild on the child categories but it doesn't close the initial child <li> so that plus all its children get highlighted see screenshot.

      And on the sneaker level it just gives me the all the grandchildren.

      Attached Image

    • 12/07/11 8:15pm

      Gabriel Reguly says:

      Hi,

      Just saw you answer to John, you need to use this code then:


      <?php
      if (is_category()) {
      $this_category = get_category($cat);
      }
      ?>
      <?php
      if($this_category->category_parent)
      $this_category = wp_list_categories('orderby=title&hide_empty=0&show_count=0&depth=0&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent."&echo=0&current_category=".$this_category->cat_ID);
      else
      $this_category = wp_list_categories('orderby=title&hide_empty=0&show_count=0&depth=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0&current_category=".$this_category->cat_ID);
      if ($this_category) {
      ?>
      <ul>
      <?php echo $this_category; ?>
      </ul>
      <?php
      }
      ?>


      Regards,
      Gabriel

    • 12/07/11 8:20pm

      xhanubis says:

      still only works partially see screen2.jpg as well as on the sneaker category it just shows grandchildren see screen 3

      Attached Image

    • 12/07/11 8:20pm

      Gabriel Reguly says:

      Hi,

      Which code are you using? The last one shall work ok.

      From the codex


      current_category
      (integer) Allows you to force the "current-cat" to appear on uses of wp_list_categories that are not on category archive pages. Normally, the current-cat is set only on category archive pages. If you have another use for it, or want to force it to highlight a different category, this overrides what the function thinks the "current" category is. This parameter added at Version 2.6


      Anyway, sorry but I need to go offline.

      Hopefuly John will be able to help you, else tomorrow I can try again.

      Regards,
      Gabriel

  • avatar
    Last edited:
    12/07/11
    7:52pm
    Mike Van Winkle says:

    Yeah, I think I'm a little confused too. Do you mean you want a listing of posts ordered in this way? or a ordered menu?

  • avatar
    Last edited:
    12/07/11
    7:53pm
    Luis Abarca says:

    Its a custom taxonomy or just categories ??

    You need the code for the menu and diplay the products ?

    • 12/07/11 8:02pm

      xhanubis says:

      Hi,

      I have added a an image which shoes what the navigation looks like at different levels. What I need to happen is that regardless of the depth you are in the categories it will always display the child categories and the grandchild categories like in fig 1.

      Attached Image

  • avatar
    Last edited:
    12/07/11
    8:04pm
    John Cotton says:

    Do you literally want the word 'CURRENT' against the current category? Or do you just want to style that item? If just style, then 'current-cat' is the class you need to work with in your stylesheet.

    Either way, if you always want the same content, just with the word CURRENT or some special styling, then why don't you just have wp_list_categories() on it's own - no need for logic to change it.

    • 12/07/11 8:06pm

      John Cotton says:

      Saw your image... you just need:



      <?php
      $shoe_cat = 7; // or whatever it is

      wp_list_categories('orderby=title&hide_empty=0&show_count=0&depth=1&title_li=&use_desc_for_title=1&child_of='.$shoe_cat."&echo=0");
      ?>
      <ul>
      <?php echo $this_category; ?>
      </ul

    • 12/07/11 8:09pm

      xhanubis says:

      Hey John,

      The issue is the site has many more categories, so i can't hard code the category ID, I just need the user to be able to navigate the category and get the child and grandchildren of the main category they are browsing whether they are viewing a child or a grandchild.

      I just need to be able to style the current category differently!

    • 12/07/11 8:15pm

      John Cotton says:

      In which case, you need to recurse up the tree


      $top_cat = get_category($cat);
      while($top_cat->parent) {
      $top_cat = get_category($top_cat->parent);
      }


      Now you can use $top_cat->term_id in wp_list_cats...!

    • 12/07/11 8:18pm

      xhanubis says:

      Not sure what to do with your code John.

    • 12/07/11 8:18pm

      John Cotton says:

      Just to be clear:



      <?php

      $top_cat = get_category( get_query_var('cat') );

      while($top_cat->parent) {
      $top_cat = get_category($top_cat->parent);
      }

      $cats = wp_list_categories('orderby=title&hide_empty=0&show_count=0&depth=1&title_li=&use_desc_for_title=1&child_of='.$top_cat."&echo=0");
      ?>

      <ul>

      <?php echo $cats; ?>

      </ul

    • 12/07/11 8:22pm

      John Cotton says:

      Sorry, that should be


      <?php
      $top_cat = get_category( get_query_var('cat') );

      while($top_cat->parent) {
      $top_cat = get_category($top_cat->parent);
      }

      $cats = wp_list_categories('orderby=title&hide_empty=0&show_count=0&depth=0&title_li=&use_desc_for_title=1&child_of='.$top_cat->term_id."&echo=0");

      ?>

      <ul>
      <?php echo $cats; ?>
      </ul>



      Lawrence! It's really hard to edit in these little text boxes!!!

    • 12/07/11 8:28pm

      xhanubis says:

      That did it! thanks a bunch dude!

      Attached Image

    • 12/07/11 8:29pm

      John Cotton says:

      My pleasure :)

    • 12/07/11 8:33pm

      xhanubis says:

      Hate to seem ungrateful but one little niggle. Because the current-cat is on the parent <li> all the grandchildren are highlighted as well as the current child category. Any Idea how to fix this?

      <ul>

      <li class="cat-item cat-item-27 current-cat"><a href="http://www.leesfifthavenue.com/shoes/kids-shoes/" title="Kids shoes">Kids</a>
      <ul class='children'>
      <li class="cat-item cat-item-109"><a href="http://www.leesfifthavenue.com/shoes/kids-shoes/dress-kids-shoes/" title="Dress shoes for kids">Dress</a>
      </li>
      <li class="cat-item cat-item-110"><a href="http://www.leesfifthavenue.com/shoes/kids-shoes/sandals-kids-shoes/" title="Sandals for kids">Sandals</a>
      </li>
      <li class="cat-item cat-item-111"><a href="http://www.leesfifthavenue.com/shoes/kids-shoes/school/" title="School shoes for kids">School</a>
      </li>
      <li class="cat-item cat-item-112"><a href="http://www.leesfifthavenue.com/shoes/kids-shoes/sneakers-kids-shoes/" title="Sneakers for kids">Sneakers</a>

      </li>
      </ul>
      </li>

    • 12/07/11 8:38pm

      John Cotton says:

      Sounds like a different question ;)

      If, in the example you give, you just want to style Kids, then you should be applying the css with a selector of '.current-cat > a' rather than just '.current-cat'. That way, any bolds, colours or whatever will only affect the link of the the current category.

    • 12/07/11 8:41pm

      xhanubis says:

      cool beans, I can figure it out from here I think. Do you get paid automatically or do I have to do something else...sorry but am a noob!

    • 12/07/11 8:48pm

      John Cotton says:

      Do you get paid automatically


      No :) You (and others) have to vote. You get the option to do that once you've closed the question.

    • 12/07/11 9:07pm

      xhanubis says:

      Ahh thats cool, I like this site. Thanks again. I voted to give you the cash. Thanks again!

This question has expired.



Gabriel Reguly, Julio Potier, Luis Abarca, Francisco Javier Carazo Gil, xhanubis voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.