Forums

Parent category not a link (14 posts)

  1. nozero
    Member
    Posted 1 year ago #

    Ive been searching online and cant find an answer to this.

    we have our category structure as so

    ->state
    -->NY
    -->CA
    -->FL

    ->Destination
    -->Park
    -->Beach
    -->Movie

    we have about four more groups. so what we want (for this example) is that "state" and "Destination" should not be a link. only the children below it should be.

    is there a way to do this using wp_list_categories()?

    is there another way to do it without using wp_list_categories()?

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    You could use get_categories and build your own custom category list.

  3. nozero
    Member
    Posted 1 year ago #

    in what way ...

  4. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

  5. nozero
    Member
    Posted 1 year ago #

    Thanks for your help!

    I wish i knew hoe to read that kind of code. all i know how to do is use the wordpress functions

  6. nozero
    Member
    Posted 1 year ago #

    ok i figured it out, thanks.

    so how do i do this that it should know which is the parent not to link it?

  7. nozero
    Member
    Posted 1 year ago #

    more clearly.

    i modified the code to this

    ' <?php
    ' $categories= get_categories('parent=0');
    ' foreach ($categories as $cat) {
    ' $xyz = $cat->cat_name;
    ' $xyz .= ' ('.$cat->category_count.')';
    ' $xyz .= '';
    ' echo $xyz;
    ' }
    ' ?>

    the point is to get the top most cats then i will loop through each one to get their children. the problem is that parent=0 doesnt work

  8. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    $cat->parent will be equal to zero for a top level category. Otherwise it will be equal to it's parent's id.

  9. nozero
    Member
    Posted 1 year ago #

    i needed to add hide_empty=0
    because the top cats dont have anything.

  10. nozero
    Member
    Posted 1 year ago #

    is there anywhere a list of the information this funstion returns like

    category_count
    cat_name
    etc....

  11. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Try using something like:

    <?php $cats = get_categories();
    echo '<pre>';
    print_r($cats);
    echo '</pre>';?>

    to see the raw data.

  12. nozero
    Member
    Posted 1 year ago #

    can you help me out with this code. i dont know php so im not sure whats goign on.

    <?php
    $categories= get_categories('parent=0&hide_empty=0');
    foreach ($categories as $cat) {
    $categorieschild = get_categories('child_of='$cat->cat_ID);
    foreach ($categorieschild as $catchild) {
    $xyz = $catchild->cat_name;
    $xyz .= ' ('.$catchild->category_count.')';
    $xyz .= '
    ';
    echo $xyz;
    }
    }
    ?>

  13. nozero
    Member
    Posted 1 year ago #

    it works great the issue is that i was missing a "." right before "$cat->cat_ID" it works great.

    $categorieschild = get_categories('child_of='$cat->cat_ID);

  14. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Something like:

    <?php
    $output = '';
    $categories = get_categories('hide_empty=0');
    if( $categories ) $output .= '<ul>';
    foreach ($categories as $cat) {
    	$child_cats = get_categories('child_of=' . $cat->cat_ID);
    	if( $child_cats) $output .= '<ul>';
    	foreach ($child_cats as $child) {
    		$output .= '<li><a href="' . get_category_link( $child->term_id ). '">' . $child->cat_name . '</a>';
    		$output .= ' ('.$child->category_count.')</li>';
    	}
    	$output .= '</ul>';
    }
    $output .= '</ul>';
    echo $output;
    ?>

    should output an unordered list of categories with only the child cats having links - but bear in mind that I haven't tested this so it might contain syntax errors.

Topic Closed

This topic has been closed to new replies.

About this Topic