Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Help with wordpress category navigation WordPress

  • SOLVED

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 } ?>

Answers (4)

2011-12-08

John Cotton answers:

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.


John Cotton comments:

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


xhanubis comments:

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!


John Cotton comments:

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...!


xhanubis comments:

Not sure what to do with your code John.


John Cotton comments:

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


John Cotton comments:

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!!!


xhanubis comments:

That did it! thanks a bunch dude!