Hey i came up with this and its almost there but it list the sub categories aswell is there any way that it dosent list the sub categories
<?php foreach((get_categories(”)) as $cat)
{
echo ‘cat_name . ‘”>’.$cat->cat_name.’
‘;
$categories= get_categories(‘child_of=’.$cat->cat_ID);
foreach ($categories as $cat2) {
$option .= $cat2->cat_name;
$option .= ‘,’;
$option .= ”;
echo $option;
}
echo ‘
‘;
}
?>
Your answer conflicts with your question.
In your first post you are asking for a way to display main categories with sub categories below it.
In your second post you are asking how to not display sub categories.
Which one do you want?
lol no ya in the second post i made some code that would show it the way i want but once its finished with listing the sub cat its then starts to list the sub-cat as main-categories like this
MAIN_CAT
sub,sub,sub
SUB_CAT
SUB_CAT
SUB_CAT
I belive the frist loop is reading the subcategories as well I only want the top level categories in the frist loop as I will do a second loop to get the sub categories
that my problem right now
do this test inside your first foreach loop….
This tests to see how many parents your category has – if the subcategory is a grandchild, it’ll just be skipped in this loop.
$parents = explode('|',get_category_parents($cat2,false,'|'));
if ($parents[2] == "") {
(do what you want here)
}
Ha it works thanks so mu i just did a few tweaks to the code you gave m its running just fine
here is the code if anyone need its
foreach((get_categories('')) as $cat)
{
$parents = explode('|',get_category_parents($cat,false,'|'));
if ($parents[1] == "") {
echo '<b>' . $cat->cat_name . '</b>'.$cat->cat_name.'<br /> ';
$categories= get_categories('child_of='.$cat->cat_ID);
foreach ($categories as $cat2) {
$option = $cat2->cat_name;
$option .= ',';
echo $option;
}
echo '<br /><br />';
}
}
Oh… sorry, I should have edited what I posted a little better – for me it’s [2] because I’m listing subchildren of a particular category.
Glad you got it worked out!
AHHHHHH it works but it wontr show more then 1 sub-cat
isnt that exactly what you wanted? you want it to show one level of subcat, right?