0
votes

Hey wonderful programmer peoples!

I have a fully functioning PHP snippet which lists categories and their posts, but we now have subcategories! I'd like to extend the current script but at a loss where to start, my PHP knowledge is quite basic but I'm learning..

The function of the script below is to:

  1. List categories.
  2. List link to post within the category

    <div class="menu-main-menu-container">
    <ul id="primary-menu" class="menu nav-menu" aria-expanded="false">
        <?php
        $cat_args = array(
            'taxonomy' => 'products-category',
        );
        $categories = get_categories($cat_args);
    
        foreach ($categories as $category) {
            $cat_query = null;
            $args = array(
                'order' => 'ASC',
                'orderby' => 'title',
                'posts_per_page' => -1,
                'post_type' => 'products',
                'taxonomy' => 'products-category',
                'term' => $category->slug
            );
    
            $cat_query = new WP_Query($args);
            ?>
            <?php
            if ($cat_query->have_posts()) {
                echo "<li class='page_item'>" . $category->name;
                echo "<ul class='custom-submenu'>";
                while ($cat_query->have_posts()) {
                    $cat_query->the_post();
                    ?>
                    <li>
                        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                    </li>
                    <?php
                }
                echo "</ul></li>";
            }
            wp_reset_postdata();
        }
        ?>        
    </ul>
    </div>
    

What I'd like to achieve is:

  1. List categories.
  2. If category has subcategory, list subcategories.
  3. List link to post within the category/subcategory.

FYI: The above is used as a menu, it currently prints as shown below: enter image description here

For example, I'd like to change the first menu item 'Bars' to include 'front bars' and 'back bars' (both subcategories) and then list their posts in further submenus.

Thank you in advance for your assistance and knowledge, I hope I have provided enough information! :)

2
Is Bar is the parent category and front bars and back bars are subcategory? if yes means, where you want to show the post which belongs to the category Bars - Ramya
Good question! Yes, 'bars' is the parent for 'front bars' and 'back bars'. The parent category will have no posts if it has subcategories, all posts will then be in the subcategories. - AlexHighHigh

2 Answers

0
votes

Spent some time with a friend creating this properly, for anyone in search of this in future please see here:

https://freshlondon.biz/wordpress-menu-from-categories-subcategories-and-posts/

Here's some previews on desktop and mobile: enter image description here enter image description here

-1
votes

I have tried my best.

On here I just make a menu based on the category. If the category has no subcategory means, I just listed its own post details under submenu and if it has subcategory means just listed the subcategory as a submenu and then listed the subcategories post under that subcategory.

I hope this will help you and which meets your requirement.

$category = get_categories();

foreach ($category as $value){
    $cat_id = $value->term_id;
    // Check if this category has a child category
    if(!empty(category_has_children($cat_id))){
        $subcategories = category_has_children($cat_id);?>
    <ul class="parent_menu">
    <?php echo $value->cat_name;
        foreach($subcategories as $subcategory_data) {
            ?>
            <li class="sub_menu">
            <?php echo $subcategory_data->name;
                // function to list all the post details
                get_category_data_post($subcategory_data->term_id);
            ?>
            </li><?php
        }
        ?>
    </ul>
    <?php
    } else{
        // Category which don't have sub category
        if($value->parent ==0) {
            ?>
            <ul class="parent_menu">
                <?php echo $value->cat_name;
                    get_category_data_post($value->term_id);
                ?>
            </ul>
            <?php
        }
    }?>
<?php
}

function category_has_children( $term_id = 0, $taxonomy = 'category' ) {
    $children = get_categories( array(
        'child_of'      => $term_id,
        'taxonomy'      => $taxonomy,
        'hide_empty'    => false
    ) );
    return ( $children );
}

function get_category_data_post($cat_id){
    $args = array(
        'category' => $cat_id
    );
    $posts = get_posts($args);
    foreach ($posts as $val) {
        ?>
        <ol class="sub_menu_2">
            <a href="<?php echo get_post_permalink($val->ID); ?>"></a><?php echo $val->post_title; ?>
        </ol>
        <?php
    }
}