Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Parent & 1st Level CHILD Category in a Drop Down List Wordpress

Tags:

php

wordpress

I currently have this code that shows all the parent categories in a drop down list.

HTML/PHP Code

<ul>
    <?php 
    $args = array(
    'orderby' => 'name',
    'hierarchical' => 1,
    'taxonomy' => 'category',
    'hide_empty' => 0,
    'parent' => 0,
    );
    $categories = get_categories($args);
    foreach($categories as $category) {
    echo '<li><a href="' . get_category_link($category->cat_ID) . '" title="' . $category->name . '">' . $category->name . '</a></li>';
    } 
    ?>
</ul>

There's no problem with the code below. Actually, it works perfectly! You can see it here at my wordpress website: www.bendaggers.com

What I want to achieve now is how can I add the 1st level child of the parent just what it shown in Image 1 below with the same effect.

Image 1 - Sample

This is what I want to achieve, whenever the user hovers on the listed Parent Category, it will display its 1st level child category as show in the image below.

enter image description here

Image 2 - Sample Parent/Category Hierarchy.

enter image description here

By the way, I need a working code PHP, HTML and CSS also.

I really appreciate you help and efforts, thank you very much!

Some additional Information that might be useful.

  • The website is a wordpress website.
  • All post are properly categorize (parents, 1st level child category is properly categorized).
like image 782
Ben Daggers Avatar asked Jul 25 '15 02:07

Ben Daggers


1 Answers

You can make function for check If the parent category has child category and pass parent term into newly created function. copy your ablove code and make a function and paste it in your custom function.

And get parent category(you should make first custom function to get paent term) and pass this term to your second custom function

like image 128
Jagdish Banda Avatar answered Sep 23 '22 02:09

Jagdish Banda