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

Category 2 tier menu help WordPress

  • SOLVED

Hi all,

I am trying to create a 2 tier menu using the code below but for some reason it does not show children even though they exist. Can anyone see what I am doing wrong? I have very limited PHP coding knowledge.

<ul id="nav">
<?php wp_list_categories('orderby=order&title_li=&depth=1'); ?>
</ul>

<?php if($post->post_parent)
$children = wp_list_categories("title_li=&child_of=".$post->post_parent."&echo=0"); else
$children = wp_list_categories("title_li=&child_of=".$post->ID."&echo=0");

if ($children) { ?>
<ul id="subnav">
<?php echo $children; ?>
</ul>
<?php } else { ?>
<?php } ?>


Thanks,
Karl

Answers (2)

2010-05-08

Oleg Butuzov answers:

code totaly wrong.

you trying to get a categories by post parents, category have own parents or childrens.


should be something like this


<ul id="nav">
<?php wp_list_categories('orderby=name&title_li=&depth=1'); ?>
</ul>


<?php

if($wp_query->query_vars['cat']) {
$children = wp_list_categories("title_li=&child_of=".$wp_query->query_vars['cat']."&echo=0");
} else {
//$children = wp_list_categories("title_li=&child_of=".$post->ID."&echo=0");
}
?>
<? if ($children) { ?>
<ul id="subnav">
<?php echo $children; ?>
</ul>
<?php } else { ?>

<?php
// do nothing?
?>

<?php } ?>



or simple

<ul id="mainnav">
wp_list_categories('orderby=name&title_li=&depth=2');
</ul>


with css


#mainnav ul { display:none}
#mainnav .current-cat ul,
#mainnav .current-cat-parent ul { display:block}


Karl comments:

Hey Oleg,

Trying your solutions now. Thanks.


Oleg Butuzov comments:

dose it help you?

2010-05-08

Milan Petrovic answers:

Try this:

<ul id="nav">
wp_list_categories('depth=2&title_li=');
</ul>


This already makes the list with given depth. I don't get what you are trying to do with middle part and getting parent from post and use it with categories.

Milan


Karl comments:

Hi Milan,

Thanks for the reply. Well I was trying to modify a tutorial I had seen to make it work with categories. You see the tut is for pages. See here: http://www.darrenhoyt.com/2008/02/12/creating-two-tiered-conditional-navigation-in-wordpress/

Essentially, I want this tut to work with categories.

Thanks for your time Milan & Oleg.

Regards,
Karl


Milan Petrovic comments:

Well, you can't. Tutorial works with pages, and they are hierarchical. Posts are not. You are mixing things here that have nothing to do with each other.

You can get category for post and use that, but again, you need to explain what you need to do, what to display for post.


Karl comments:

I really need to replicate the tut but for cats. So when parent cat is selected it also shows 2nd tier cats related to selected parent. so when on a post I want to show parent and child cats of that post active.