• Hi,

    Can anyone help me with displaying categories in post as drop down box?

    I saw a plugin, but it is displaying all categories in one drop down box.

    I want it to be displayed like three drop down boxes

    one is for main categories (parent category)
    second one is for sub category
    third one is even more sub category

    Any piece of advice would be helpful

Viewing 1 replies (of 1 total)
  • Hello,
    Yes, you can use get_categories() using ‘child_of’ attribute. for example all sub categories of category with the ID of 17:

    [ Moderator note: code fixed. Please wrap code in the backtick character or use the code button. ]

    $args = array('child_of' => 17);
    $categories = get_categories( $args );
    foreach($categories as $category) {
        echo '<p>Category: <a>term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
        echo '<p> Description:'. $category->description . '</p>';
        echo '<p> Post Count: '. $category->count . '</p>';
    }

    this will get all categories that are descendants (i.e. children & grandchildren) .

    if you want to display only categories that are direct descendants (i.e. children only) the you can use ‘parent’ attribute.

    $args = array('parent' => 17);
    $categories = get_categories( $args );
    foreach($categories as $category) {
        echo '<p>Category: <a>term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
        echo '<p> Description:'. $category->description . '</p>';
        echo '<p> Post Count: '. $category->count . '</p>';
    }

    Thanks

Viewing 1 replies (of 1 total)
  • The topic ‘Show categories in drop down’ is closed to new replies.